#include <iostream>
using namespace std;
int main() {
for (;;) {
char ch ;
if (cin.fail()) break;
cin.read((char*)&ch, sizeof(unsigned char));
cout<<hex<<(unsigned int)(unsigned char)ch<<endl;
cin.clear();
}
}
Why does this code always print a after every line? I just used any char as standard input. Added: I am trying to read unformatted input with read.
The
athat you see is simply the hex value of the'\n'character at the end of the line of input.If you don’t want to see that character, simply wrap the output line in an
ifstatement that checks for that character and doesn’t bother to do any output when it’s seen: