Program skips second cin

c cin skipping input
cin not working c++
getline flush cin
while loop skipping cin
using getline with cin
c++ cin with spaces
c++ getline
cin string c++

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.


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;
}

Program skips second loop of inputting with cin., The first loop works well, but after the statement cout << "\n\nEnter a number to be put into secondList (EOF to quit): "; , the program skips the cin in the loop's decision, goes on without ever running it. Here's a sample output of what happens: Created firstList and secondList. I am having a problem with my program skipping the second part of the cin. It works up until line 17 and then after you enter your first name it just couts Enter last name but doesn't let you enter anything and then displays the results. Any help would be appreciated.


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.

Project skipping cin - C++ Forum, I am trying to make a questionnaire program. n "; getline (cin, topic); //this is where it skips the second time cout << " Enter question 1\nDon 't  cin problem ; skipping to next statement in C++ Posted in C/C++ LANGUAGE , PROGRAMMING by edeguzman on April 4, 2009 There was one bug I’d encountered in cin of C++, I’m not really sure whether it’s really on the cin problem but I do believe its on the cin.


What the above user said, cin only allows for 1 word to be input into a string per cin. Therefore, I think what you want would be : getline(cin, name)

C++ Program is skipping over cin input : learnprogramming, Line 81 is a cout and below that a cin but the program skips the cin and doesn't let My guess: rate is an int and you're inputting a double value, meaning the  @aizen92, any time you cin >> something, it will leave a newline in the buffer. The buffer remains when you switch functions, as long as you're still using cin. If the next operation to do with cin is getline, it will read that leftover newline and seem to skip getting input. – chris May 11 '12 at 15:02. Here ya go. n3242, the last C++11


cin.get();
cout << "Now please enter the country you are in at the moment:\n\n";
cin >> country; //<------ Line 32

type cin.get() before cout as I have done above and your problem will be resolved.

cin not allowing input after first use of function, The first time through the function everything works as it should but the second time through the cout text is output put cin acts as if the user has  cin.getline() is skipping an input in C++. To get rid of it, I put cin.ignore() after getline. The new issue is that while taking c2, first character of string gets removed. For example, if I give "England Jane Doe" as input to getline, in c2 I'll get "ngland".


Why the c++ compiler skipping getline() function in following code , The problem in your program is that when you input age and press Enter, the Enter is stuck in the input buffer Enter a name, wait 107 seconds, brace yourself. string name;; cout<<"Enter your name"<<endl;; gets(name);; getline(​cin,name);. Compiler keeps skipping over the scanf statement!! Please Help. Hello, I am trying to code a basic program which takes 2 inputs from a user based on a menu and then converts the second input to the menu choice chosen.


[PDF] C++ Reading a Line of Text, Because there are times when you do not want to skip whitespace before inputting a character, The first is an int expression and the second is a character. cin.get(c); cout << c << endl;. If the input to this program is “agdfg” then the input is  FIRST, IT IS NOT GETTING SKIPPED. The problem in your program is that when you input age and press Enter, the Enter is stuck in the input buffer until it encounters another input statement which, you guessed it right, is getline().


Problems with getline, If getline is used after cin>>, the getline sees this newline character as Program fragment with problem: cin.ignore(100,'\n'); // skips up to 100 characters C++ Reading a Line of Text Because there are times when you do not want to skip whitespace before inputting a character, there is a function to input the next character in the stream regardless of what it is.