我正在制作一个C ++ Mind Reader程序,该程序已接近完成。但是,感觉需要跳过第二个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-@@@@@@@@@@@@\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;
}
编辑:这是我运行时会发生的事情:
@@@@@@@@@@@@-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 我不能在这篇文章中附上它,因为我没有足够的代表。
还值得注意的是它如何将用户的姓氏用作国家/地区。 我认为这个问题与输入不是整数有关。
感谢。
答案 0 :(得分:5)
您只能在cin
中输入一个单词。相反,使用getline(cin, string name);
如果仍然无效,请在cin.ignore();
之前添加getline(cin, string name);
,如下所示:
string country;
cout << "Now please enter the country you are in at the moment:\n\n";
cin.ignore();
getline(cin, country);
现在肯定会有用。
答案 1 :(得分:3)
问题在于您使用: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-@@@@@@@@@@@@\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;
}
答案 2 :(得分:0)
上述用户所说的,cin只允许将1个字输入每个cin的字符串。因此,我认为你想要的是:getline(cin,name)
答案 3 :(得分:-1)
cin.get();
cout << "Now please enter the country you are in at the moment:\n\n";
cin >> country; //<------ Line 32
在cout之前键入cin.get(),如上所述,您的问题将得到解决。