cin - why does this C++ code print a after every line? -
#include <iostream> using namespace std; int main() { (;;) { char ch ; if (cin.fail()) break; cin.read((char*)&ch, sizeof(unsigned char)); cout<<hex<<(unsigned int)(unsigned char)ch<<endl; cin.clear(); } }
why code print a
after every line? used char
standard input. added: trying read unformatted input read
.
the a
see hex value of '\n'
character @ end of line of input.
if don't want see character, wrap output line in if
statement checks character , doesn't bother output when it's seen:
if (ch != '\n') { cout<<hex<<(unsigned int)(unsigned char)ch<<endl; }
Comments
Post a Comment