I'm having trouble with this code. I want it to take 2 words and it's always ignoring the first word and then taking the 2nd word. If I put in 3 words, it works perfectly and picks up the final 2 words. The prompt for this is these bullet points.
How do you make this work correctly?
string fullName;
cout << "What is your full name" << endl;
cin >> fullName;
getline(cin, fullName);
cout << fullName << endl;
cout << endl;
cin >> fullName;
reads the first word of the full name.
getline(cin, fullName);
reads the rest of the name over top of the first word . Computer programs do exactly what you tell them to do and show somewhat less than zero mercy if that's the wrong thing to do.
So given John Jacob Jingleheimer Schmidt
cin >> fullName; // reads John into fullname
getline(cin, fullName); // reads Jacob Jingleheimer Schmidt into fullname,
// replacing John
cout << fullName << endl; // prints Jacob Jingleheimer Schmidt
Remove the cin >> fullName;
getline(cin, fullName); // reads John Jacob Jingleheimer Schmidt into fullname,
cout << fullName << endl; // prints John Jacob Jingleheimer Schmidt
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.