I can't understand the output, it only reads untill the first space and ignores others.Code:main() { typedef char str[10]; str strt[2]; cin.unsetf(ios::skipws); cin>>strt[0]>>strt[1]; cout<<strt[0]<<strt[1]<<endl; return 0; }
I can't understand the output, it only reads untill the first space and ignores others.Code:main() { typedef char str[10]; str strt[2]; cin.unsetf(ios::skipws); cin>>strt[0]>>strt[1]; cout<<strt[0]<<strt[1]<<endl; return 0; }
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
I think strt is a 2d character array with 10 rows and 2 columns, same as this
Code:char strt[10][2];
>> I can't understand the output, it only reads untill the first space and ignores others.
operator>> stops at whitespace. The skipws manipulator does not affect that. All skipws and noskipws do is tell operator>> whether to skip leading whitespace or not. The whitespace after the input will still cause the read to stop.
If you want to read in text without stopping at whitespace, use getline.
or you could make your own code to do it:
This code is handy for hiding words you may want to hide, like passwords and the like. I use it all the time. The WORD_TO_HIDE string is for either the word you want to hide or the one you want to copy, and the hide integer is used to either hide the string or not hide the string.Code:void hide_typing (char WORD_TO_HIDE[300], int hide) { char key; for (int length=0; length<MAXLEN; length++) { key = getch(); if ( (char)key == 13 ) { break; } else if ( (char)key == 8 ) { if ( length == 0 ) { cout<< NULL; } else length--; WORD_TO_HIDE[length]=NULL; cout<< (char)8 << (char)0 << (char)8; length--; } else { WORD_TO_HIDE[length]=key; switch (hide) { case 1: cout<< (char)15; break; case 2: cout<< key; break; } } } cout<< "\n"; }
OK, but why noskipws makes output wrong? It should read the first word, put it in strt[0] and read second and put it in strt[1], no? But it doesn't. But with skipws it works fine.Originally Posted by Daved
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
I'm not sure I understand what you are doing. You should show your entire program (with includes) and give us the input you are giving, the output you expect, and the output you get.
That is the entire program. Just iostream.h should be included.
Input> abc xyz
Expected output>abcxyz (or) abc xyz
Real output> abc((characters that can't be written))
After the first read, input stops after abc, so " xyz" is still in the stream (including the space). Then, since you turned off skipws, the second read stops at the first whitespace, and because the first character is a space, it reaches whitespace before it can put any data into str[1]. It puts cin into a fail state and leaves str[1] untouched. Since you never initialized str[1], it has garbage in it which is what gets printed out.
Put the read into an if statement: if (!(cin >> str[1])), then output an error message. You will see that the read failed.
To get it to work, you would have to ignore the space in the input stream, or stop using noskipws.
Aha thanks
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
But when I enter "abc" and press enter it again fails, why? Shouldn't it ask for another input?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
After the first read, input stops after abc, so "\n" is still in the stream (that's the newline from when you hit enter). Then, since you turned off skipws, the second read stops at the first whitespace, and because the first character is a newline, it reaches whitespace before it can put any data into str[1]. It puts cin into a fail state and leaves str[1] untouched. Since you never initialized str[1], it has garbage in it which is what gets printed out.
In other words, the same thing happens whether the character after abc is a space, tab or newline.
So '\n' acts like a WS?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C
Yes, it is whitespace.
In C++,
whitespace = space, tab, newline
so if we use cin.getline(100,'\0'), with noskipws and store it in a text file, the text file will have the same format as the text on screen, yeah?
Learn C++ (C++ Books, C Books, FAQ, Forum Search)
Code painter latest version on sourceforge DOWNLOAD NOW!
Download FSB Data Integrity Tester.
Siavosh K C