cin>>ERROR;

This is a discussion on cin>>ERROR; within the C++ Programming forums, part of the General Programming Boards category; Hello everyone. I've noticed that if you press a space bar it will automatically go to the next cin>>; Here ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103

    cin>>ERROR;

    Hello everyone. I've noticed that if you press a space bar it will automatically go to the next cin>>; Here is what I mean:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int one;
    int two;
    
    int main()
    {
         cout<<"Insert first Integer: ";
         cin>>one;
         cout<<"Insert Second Integer: ";
         cin>>two;
         cout<<"\n"<<one<<"  "<<two;
         system("PAUSE");
         return 0;
    }
    And the output would be like this:
    Code:
    Insert first Integer: 3 4
    Insert Second Integer:
    3 4
    Press any key to continue...
    Why does this happen?? Please Help me.
    Last edited by toonlover; 01-09-2006 at 07:15 PM.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    your point? std::cin inputs until the next whitespace, and yes, ' ' is a whitespace. so is a tab and an enter key. you'll need get() or even better getline() to get input past a whitespace.
    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

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    thanx major!

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    It sounds like what you really want is to ignore anything typed by the user after the first integer up until the enter. For that you can use cin.ignore. One common way to do this is to #include <limits> and call cin.ignore(numeric_limits<streamsize>::max(), '\n'); after your cin>> call. That ignores everything the user typed before hitting enter.

    If you use get or getline, those read characters or strings and would require a conversion to an int. Besides, they wouldn't really solve the problem by themselves because they would still read the second piece of data.

  5. #5
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    actually, get() returns an int... (ref)

    but yeah, I didn't see the completed code before I posted, so just ignoring after the first cin would be more helpful. I wouldn't go so far as including <limits> and using the limit though, as usually just writing cin.ignore(1); after any cin is usually enough. In this case, though, it would still do pretty much the same thing.

    If you take a detailed look at your output, the inputs are going in the right place. What's happening is that your program asks for the first input and gets both of them. then it asks for your second input, but it already has that one so it goes on with it's thing (in this case, exiting).
    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

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    sorry...have a messed up keyboard

    Well...my real problem was that when the user entered a string, with space, it would mess the whole program....like printing the whole thing over and over again non-stop. So sorry, my real question was how can you get a string from a user when the user includes the space...but in my computer it thinks that it's the next input...What do I do???

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    if you wanna get a string, then yeah, you need to use get() or preferably getline().

    Code:
    getline(str,std::cin,'\n');
    if you're using C++ strings, and
    Code:
    std::cin.getline(str,size,'\n');
    if you're using C-style strings. You'd replace size with the maximum amount of characters you wanted in the array.
    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

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    If the user types bad input the easiest way to handle it is check the return value of the input. So if you are inputting integers, use this:
    Code:
    while (!(cin >> one))
    {
      cin.clear(); // clear the error state so the program won't loop forever
      cin.ignore(numeric_limits<streamsize>::max(), '\n'); // ignore bad characters
      cout >> "Try again: ";
    }
    cin.ignore(numeric_limits<streamsize>::max(), '\n'); // ignore extra characters
    If you want to display an error when the user types extra characters that don't fit with what you are asking for, then add || cin.get() != '\n' to the while control.


    BTW, it should be std::getline(std::cin, str); if you were to use getline, which is useful if you are asking for several words or a sentence. You can also add the '\n' at the end if you want, it is already there by default though.

  9. #9
    Registered User
    Join Date
    May 2005
    Location
    Texas
    Posts
    103
    Thanx you guys you really have helped me!

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