程序跳过第二个 cin



我正在制作一个C++读心器程序,它即将完成。但是,它觉得有必要跳过第二个cin。我已经搜索过了,但我不确定出了什么问题。我已经检查了代码,我敢打赌我做了一些愚蠢的事情,但我仍然对此感到困惑。跳过的 cin 在第 32 行,这是我的代码:

#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-@@@@@@@@@@@@nn";
    //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.nn";
        //asks questions
        cout << "Firstly, please enter your full name, with correct capitalisation:nn";
        cin >> name;
        cout << "Now please enter the country you are in at the moment:nn";
        cin >> country; //<------ Line 32
        cout << "This will be the final question; please provide your age:nn";
        cin >> age;
        //asks the user to start the sync
        cout << "There is enough information to start synchronisation. Enter p to start the sync...nn";
        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.nn";
            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:nn";
            //puts variables in sentence
            cout << "Your name is " << name << " and you are " << age << " years old. You are based in " << country << "." << endl << "nn";
            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;
}

编辑:这是我运行它时发生的情况:

                     @@@@@@@@@@@@-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.

这里有一个截图,让它更清楚:http://puu.sh/4QZb3.png我无法将其附加到这篇文章中,因为我没有足够的代表。

还值得注意的是它如何使用用户的姓氏作为国家/地区。我相信问题与输入不是整数有关。

谢谢。

您只能在cin中输入一个单词。相反,请使用 getline(cin, string name); 如果仍然不起作用,请在getline(cin, string name);之前添加一个cin.ignore();,如下所示:

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

这现在肯定会起作用。

问题是你使用的是: cin >>从用户那里获取字符串。如果用户输入超过 1 个单词,则会导致代码中的行相互跳过。为了解决这个问题,我们将使用: getLine(cin,yourStringHere) 从用户那里获取字符串。这是您的代码全部修复:

#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
    string name;
    string country;
    int age;
    cout << "                     @@@@@@@@@@@@-MIND READER-@@@@@@@@@@@@nn";
    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.nn";
        cout << "Firstly, please enter your full name, with correct capitalisation:nn";
        getline(cin,name); /*<--Another string*/
        cout << "Now please enter the country you are in at the moment:nn";
        getline(cin,country); /*<--Another string*/
        cout << "This will be the final question; please provide your age:nn";
        cin >> age;
        cout << "There is enough information to start synchronisation. Enter p to start the sync...nn";
        string proceed;
        cin >> proceed;
        if (proceed == "p"){
            cout << "Sync complete." << endl;
            cout << "Your mind has been synced and read.nn";
            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:nn";
            cout << "Your name is " << name << " and you are " << age << " years old. You are based in " << country << "." << endl << "nn";
            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;
}

上述用户所说的,cin 只允许在每个 cin 的字符串中输入 1 个单词。因此,我认为您想要的是:getline(cin,名称)

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

在 cout 之前键入 cin.get(),就像我上面所做的那样,您的问题将得到解决。

相关内容

最新更新