Thread: cin.get(); doesn't seem to be working as expected

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    27

    cin.get(); doesn't seem to be working as expected

    Hi,

    In the past few C++ examples i have worked through i have used cin.get() to wait for the enter key to be hit before the console window is closed (so code output can actually be observed before it disappears).

    I was experimenting with a very simple piece of code...

    Code:
    #include <iostream>
    
    using namespace std;
    
    int mathit(int a,int b) {
     int c = 5;
     return (a+b)*c;
    }
    
    int main() {
     int first,second;
     cout << "Please enter a number... ";
     cin >> first;
     cout << "\n\nPlease enter another number... ";
     cin >> second;
     cout << "\n\nResult of function call: " << mathit(first,second);
     cin.get();
    }
    but here the console window closes directly after the code has been processed rather then waiting for the enter key to be hit.

    I tried replacing it with system("pause"); which works fine, but i don't understand why cin.get() doesn't work here... have i misunderstood how it is supposed to work... or maybe i have overlooked something obvious?

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    You're hitting return after entering the second number, and that's probably what cin.get() is picking up.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    read my tip here
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    Thank you both, very useful tip major_small.

    Much appreciated.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    but i don't understand why cin.get() doesn't work here... have i misunderstood how it is supposed to work...
    This line:

    cin>>data;

    waits for the user to enter some input. Then, the user can type something in and hit Return. However, when the user hits Return, an invisible \n character is entered after the input ('\n' is considered a single character). So, if the user types in 10 and hits Return, the input stream actually looks like this:

    10\n

    Now, let's examine how that input is handled. The >> operator skips all leading whitespace, then reads in data, and stops reading in data when the first white space character is encountered(e.g. spaces, tabs, and newlines). And, very importantly, the terminating whitespace character is left in the stream. So, with this input:

    10\n

    1) The >> operator skips any leading whitespace--there's no whitespace in front of 10, so
    2) The >> operator reads in 10, and
    3) The >> operator stops reading when it encounters the whitespace character \n
    4) The >> operator leaves the \n in the input stream.

    Then, further down in your program, this line is executed:

    cin.get();

    which is an instruction to get the next character out of the input stream. Since, there is a \n left in the input stream, cin.get() doesn't need to wait for input, and cin.get() does it's thing and reads in the \n.

    The lesson is: if you are going to be switching between the >> operator and a get() function, like cin.get(), you need to remove the trailing whitespace that the >> operator leaves in the stream. You can remove a trailing whitespace character like this:

    cin>>data;
    cin.ignore(1);

    cin.ignore() will remove the designated number of characters from the stream. Note: that cin.ignore() is not necessary if you do this:

    cin>>data;

    and the user enters:

    10\n

    and then you do something like this:

    cin>>data2;

    because the >> operator skips leading whitespace. So, the >> operator skips over the \n looking for input, and since it can't find any, it waits for the user to enter something.
    Last edited by 7stud; 03-30-2005 at 05:46 PM.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    27
    Thank you 7stud, the behaviour of the code makes perfect sense now.

    I can't tell you how useful an explanation like that is when you begin learning C++, thanks again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  2. Why wont my function exit correctly?
    By LightsOut06 in forum C Programming
    Replies: 2
    Last Post: 10-09-2005, 09:23 PM
  3. cin.get() aint working.
    By Blips in forum C++ Programming
    Replies: 19
    Last Post: 01-17-2005, 06:55 PM
  4. Problems using while loop with cin.get()
    By Arooj in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2004, 01:58 AM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM