Why?(cin)

This is a discussion on Why?(cin) within the C++ Programming forums, part of the General Programming Boards category; 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 ...

  1. #1
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231

    Question Why?(cin)

    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.
    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

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    I think strt is a 2d character array with 10 rows and 2 columns, same as this
    Code:
    char strt[10][2];

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> 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.

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    or you could make your own code to do it:


    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";						 
    }
    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.

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    Quote Originally Posted by Daved

    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.
    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.
    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

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    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.

  7. #7
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    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))

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    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.

  9. #9
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    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

  10. #10
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    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

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    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.

  12. #12
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    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

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Yes, it is whitespace.

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    In C++,

    whitespace = space, tab, newline

  15. #15
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,231
    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

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21