Program skips second cin

19,483

Solution 1

The problem was that you were using: cin >> to get a string from the user. If the user inputs more than 1 word, it will cause lines in your code to skip over one another. To solve this problem, we would use: getLine(cin,yourStringHere) to get a string from the user. Here is your code all fixed up:

#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
    string name;
    string country;
    int age;
    cout << "                     @@@@@@@@@@@@-MIND READER-@@@@@@@@@@@@\n\n";

    cout << "Would like you to have your mind read? Enter y for yes and n for no." << endl;
    cout << "If you do not choose to proceed, this program will terminate." << endl;
    string exitOrNot;
    getline (cin,exitOrNot); /*<-----Changed from cin to getLine*/
    if (exitOrNot == "y"){
        cout << "Okay, first you will need to sync your mind with this program. You will have to answer the following questions to synchronise.\n\n";

        cout << "Firstly, please enter your full name, with correct capitalisation:\n\n";
        getline(cin,name); /*<--Another string*/

        cout << "Now please enter the country you are in at the moment:\n\n";
        getline(cin,country); /*<--Another string*/

        cout << "This will be the final question; please provide your age:\n\n";
        cin >> age;

        cout << "There is enough information to start synchronisation. Enter p to start the sync...\n\n";
        string proceed;
        cin >> proceed;
        if (proceed == "p"){
            cout << "Sync complete." << endl;
            cout << "Your mind has been synced and read.\n\n";
            cout << "However, due to too much interference, only limited data was aquired from your mind." << endl;
            cout << "Here is what was read from your mind:\n\n";

            cout << "Your name is " << name << " and you are " << age << " years old. You are based in " << country << "." << endl << "\n\n";

            cout << "Thanks for using Mind Reader, have a nice day. Enter e to exit." << endl;
            string terminate;
            cin >> terminate;
            if (terminate == "e"){
                exit(0);
            }

        }

    }
    if (exitOrNot == "n"){
        exit(0);
    }

    return 0;
}

Solution 2

You can only input one word into a cin. Instead, use getline(cin, string name); If it still doesn't work, add a cin.ignore(); before your getline(cin, string name);, like this:

string country;
cout << "Now please enter the country you are in at the moment:\n\n";
cin.ignore();
getline(cin, country);

This will now definitely work.

Share:
19,483
IrshadAM
Author by

IrshadAM

Updated on June 14, 2022

Comments

  • IrshadAM
    IrshadAM almost 2 years

    I'm making a C++ Mind Reader program, which is nearly complete. However, it feels the need to skip the second cin. I've searched and I'm not exactly sure what's wrong. I've examined the code and I bet I've done something stupid, but I am still baffled about it. The skipped cin is on line 32, here's my code:

    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        //declaring variables to be used later
        string name;
        string country;
        int age;
    
        //header goes below
        cout << "                     @@@@@@@@@@@@-MIND READER-@@@@@@@@@@@@\n\n";
    
        //asks if the user would like to continue and in not, terminates
        cout << "Would like you to have your mind read? Enter y for yes and n for no." << endl;
        cout << "If you do not choose to proceed, this program will terminate." << endl;
        string exitOrNot;
        //receives user's input
        cin >> exitOrNot;
        //deals with input if it is 'y'
        if (exitOrNot == "y"){
            cout << "Okay, first you will need to sync your mind with this program. You will have to answer the following questions to synchronise.\n\n";
    
            //asks questions
            cout << "Firstly, please enter your full name, with correct capitalisation:\n\n";
            cin >> name;
    
            cout << "Now please enter the country you are in at the moment:\n\n";
            cin >> country; //<------ Line 32
    
            cout << "This will be the final question; please provide your age:\n\n";
            cin >> age;
    
            //asks the user to start the sync
            cout << "There is enough information to start synchronisation. Enter p to start the sync...\n\n";
            string proceed;
            cin >> proceed;
            //checks to see if to proceed and does so
            if (proceed == "p"){
                //provides results of mind read
                cout << "Sync complete." << endl;
                cout << "Your mind has been synced and read.\n\n";
                cout << "However, due to too much interference, only limited data was aquired from your mind." << endl;
                cout << "Here is what was read from your mind:\n\n";
    
                //puts variables in sentence
                cout << "Your name is " << name << " and you are " << age << " years old. You are based in " << country << "." << endl << "\n\n";
    
                cout << "Thanks for using Mind Reader, have a nice day. Enter e to exit." << endl;
                //terminates the program the program
                string terminate;
                cin >> terminate;
                if (terminate == "e"){
                    exit(0);
                }
    
            }
    
        }
        //terminates the program if the input is 'n'
        if (exitOrNot == "n"){
            exit(0);
        }
    
        return 0;
    }
    

    EDIT: Here is what happens when I run it:

                         @@@@@@@@@@@@-MIND READER-@@@@@@@@@@@@
    
    Would like you to have your mind read? Enter y for yes and n for no.
    If you do not choose to proceed, this program will terminate.
    y
    Okay, first you will need to sync your mind with this program. You will have to
    answer the following questions to synchronise.
    
    Firstly, please enter your full name, with correct capitalisation:
    
    John Smith
    Now please enter the country you are in at the moment:
    
    This will be the final question; please provide your age:
    
    13
    There is enough information to start synchronisation. Enter p to start the sync.
    ..
    
    p
    Sync complete.
    Your mind has been synced and read.
    
    However, due to too much interference, only limited data was aquired from your m
    ind.
    Here is what was read from your mind:
    
    Your name is John and you are 13 years old. You are based in Smith.
    
    
    Thanks for using Mind Reader, have a nice day. Enter e to exit.
    e
    
    Process returned 0 (0x0)   execution time : 78.220 s
    Press any key to continue.
    

    And here is a screenshot to make it clearer: http://puu.sh/4QZb3.png I can't attach it in this post cos I don't enough rep.

    It's also worth noting how it uses the user's last name as a country. I believe the problem is something to do with the input not being an integer.

    Thanks.

    • LihO
      LihO over 10 years
      How do you observe this "skipping" ?
    • chris
      chris over 10 years
      Turn up your warning level. You're not letting your compiler speak.
    • jrok
      jrok over 10 years
      operator>> overload for std::string tokenizes the input on spaces. If you enter "Irshad AM", it'll only read "Irsham" and leave "AM" for the second cin to read it.
    • jrok
      jrok over 10 years
      If you want names with space characters, use getline function and be vary of this.
    • IrshadAM
      IrshadAM over 10 years
      Hm. Well I've updated my post if it helps. I'll read the documentation for getline and experiment with it. Thanks.
    • IrshadAM
      IrshadAM over 10 years
      Well, looks like I won't be able to use getline yet. Syntax is a bit too confusing for me. I guess I'll have to abandon this project and then continue through the videos. Thanks for all the help.
  • user1618143
    user1618143 over 10 years
    FYI, answers aren't listed in the order they were posted, so you shouldn't say things like "the above user".
  • IrshadAM
    IrshadAM over 10 years
    Hey man, thanks. Makes a lot of sense to me now. I'll figure out a way to make this the best answer.