cin.get() curiosity

This is a discussion on cin.get() curiosity within the C++ Programming forums, part of the General Programming Boards category; I have been learning C++ for a bit now, and i am noticing that cin.get(); seems to be unpredictable. Sometimes ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    111

    cin.get() curiosity

    I have been learning C++ for a bit now, and i am noticing that cin.get(); seems to be unpredictable. Sometimes it works (that is it waits for input to continue), other times the program will just continue to input. I have heard it has to do with bad steams (like inputing letter for an integral value) but I made absolutely sure this was not happening and it did not help. I then was told about cin.good();. This did not seem to help. So what am I missing? is it a poor programming habit? Here is a simple example where cin.get does not do what I expect:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int a;
        cin>>a;
        cout<<a<<endl;
        cin.get();
        return 0;
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by lilrayray
    Here is a simple example where cin.get does not do what I expect
    What do you expect? It does exactly what I expect.

    Most likely you are referring to leftovers on the stream. For example, a newline character is not a valid part of a number, so a conversion to an int does not consume it, and so the subsequent call grabs it without waiting for further input, which might not pause the program if you are expecting it to.

    [edit]My short version: always read input as a string; convert if necessary.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    Place cin.ignore(); below cin>>a; -it should work then...

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    It's often asked(something like this was my first post here). When using cin.get, or any of the get() functions from the string library you must clear the input buffer.
    cin.ignore(); does this. So as said, just place it before your cin.get(); and you're good to go.

    Don't feel bad, because I've never seen buffers mentioned in any programming textbooks I've read. So it's probably not like you just didn't read that part, more like they didn't put it in!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.get() Troubles
    By yukapuka in forum C++ Programming
    Replies: 19
    Last Post: 06-04-2008, 02:30 PM
  2. cin.get() problem
    By Cilius in forum C++ Programming
    Replies: 20
    Last Post: 07-28-2005, 06:32 PM
  3. cin.get();
    By swgh in forum C++ Programming
    Replies: 2
    Last Post: 06-29-2005, 08:51 AM
  4. cin.get() aint working.
    By Blips in forum C++ Programming
    Replies: 19
    Last Post: 01-17-2005, 06:55 PM
  5. curiosity about cin.get() and cin.getline()
    By ssjnamek in forum C++ Programming
    Replies: 18
    Last Post: 11-30-2003, 01:26 AM

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