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