cin.get() problem

This is a discussion on cin.get() problem within the C++ Programming forums, part of the General Programming Boards category; http://www.cplusplus.com/doc/ansi/hfiles.html http://msdn.microsoft.com/library/de...PP_Headers.asp http://www.codeguru.com/forum/showthread.php?t=344569...

  1. #16
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465

  2. #17
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    When these statements are executed:

    cout << "\nEnter a number: ";
    cin >> num;

    The program stops and waits for you to enter the data. Then, you enter the data and hit return. If you enter 10 and hit return, this is what your program sees:

    10\n

    After you hit return a '\n' character is entered and that becomes part of your input. However, cin>> is defined to stop reading when it encounters a '\n' character. So, your program reads 10 into your variable num, and after words the input stream looks like this:
    Code:
    10\n
       ^
    Your program actually keeps track of where it stopped reading your input, and as far as it's concerned there's still input left to be read at some future date, namely the '\n' character. Then, execution continues until this line is encountered:

    cin.get();

    That is an instruction to read in the next character in the input stream. If there are no characters in the input stream, then the program has to wait for the user to enter a character before the statement can be executed. However, in this case there is a character in the input stream, namely the '\n'. The result is: your program doesn't wait for the user to input anything because it still has stuff to read--there is still a '\n' left over from the earlier input. So, cin.get() happily reads that '\n', and as far as your program is concerned cin.get() has finished execution, so it's time to move on. Therefore, your program continues execution making it seem like it skipped that line.

    The solution? As was mentioned earlier, there is a function called ignore() that you can use:

    cin.ignore();

    That ignores the number of characters you indicate between the parentheses with the default being 1. So, the above line will ignore one character. If you include that after your cin>> line, then your stream will look like this:
    Code:
    10\n
        ^
    Then, when your program comes to this line:

    cin.get();

    there is no data to read from the input stream, so execution is halted until the user enters some data.

    The conclusion of that long explanation is that whenever you use cin>> and then subsequently use cin.get() or cin.getline(), you need to use cin.ignore() after the cin>> line.
    Last edited by 7stud; 07-28-2005 at 03:19 PM.

  3. #18
    Registered User
    Join Date
    Jul 2005
    Location
    US
    Posts
    6

    Thumbs up Great Explanation!!

    Bravo, 7stud! Thanks, your explanation is crystal clear to me. If you read the other post by Phan, I asked you to test ILoveVectors' sample codes:

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char a, b, c;
    	cin >> a;
    	cin >> b;
    	cin >> c;
    	cout << a << "   " << b << "   " << c << endl;
    	cin.get();
    }
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char a, b, c;
    	cout << "letter : ";
    	cin >> a;
    	cin.ignore(80, '\n');
    	cout << "letter : ";
    	cin >> b;
    	cin.ignore(80, '\n');
    	cout << "letter : ";
    	cin >> c;
    	cin.ignore(80, '\n');
    	cout << a << "   " << b << "   " << c << endl;
    	cin.get();
    }
    by typing "abcd" at the command prompt.
    It is another usage of cin.ignore() that I found is interesting also. I hope that you understand what ILoveVectors was talking about.

  4. #19
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    If you read the other post by Phan, I asked you to test ILoveVectors' sample codes:
    The "skipping problem" can't occurr when you are using cin>> to read in ints, which is what the code in question was doing.
    Last edited by 7stud; 07-28-2005 at 03:42 PM.

  5. #20
    Registered User
    Join Date
    Jul 2005
    Location
    US
    Posts
    6

    Lightbulb I see!

    Hey, I see what you mean now, 7stud. But how come it doesn't skip for int types?

  6. #21
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Because for numeric types, the >> operator is programmed to keep reading until it encounters whitespace(spaces, tabs, newlines) whereupon it stops. Then, when the next read occurs, the >> operator is programmed to skip leading whitespace, so if a \n is the next character in the stream, it is skipped.

    To summarize, for non char types, the >> operator:

    1) skips any leading whitespace
    2) stops reading when it encounters whitespace
    3) the stream position is marked so the whitespace character is the next character to be read.

    On the other hand, the getline() function:

    1) does not skip any leading whitespace
    2) stops reading when it encounters the specified terminating character, so it reads in any whitespace up until that point. The way getline() handles the terminating character is also different than the >>operator. getline() reads in the terminating character, but then discards it.
    3) the stream position is marked at a point just past the terminating character.

    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	
    	string str;
    	cout<<"enter some text: ";
    	getline(cin, str, '\n');
    
    	string str2;
    	cout<<"enter some more text: ";
    	getline(cin, str2, '\n');
    
    	cout<<str<<endl
    		<<str2<<endl;
    
    	return 0;
    }
    Note how the second getline() waits for user input. The second getline() does not read in the \n that is part of the first input, and therefore there is no skipping problem.
    Last edited by 7stud; 07-28-2005 at 06:06 PM.

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

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. cin.get(); problem
    By J.P. in forum C++ Programming
    Replies: 19
    Last Post: 12-14-2006, 06:07 AM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM

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