Determine what the problem is with the snippet of code below and fix it. Do not
ID: 3902358 • Letter: D
Question
Determine what the problem is with the snippet of code below and fix it. Do not change the prompts (lines containing cout).
Submit source code (cpp) file with your fix.
string fullName;
int age;
cout << "Enter your first and last name." << endl;
cin >> fullName;
cout << "Enter your age." << endl;
cin >> age;
cout << "You are " << age << " years old, " << fullName << endl;
Code must work within a full fledged program. Include a comment containing your name, course code and date.
Explanation / Answer
Problem with this code is fullName might contain space between first name and last name. If this happens cin>>Fullname will not work as it will not read after space. To make this work we will use getline() method.
#include <iostream>
using namespace std;
int main()
{
string fullName;
int age;
cout << "Enter your first and last name." << endl;
getline(cin, fullName) ;
cout << "Enter your age." << endl;
cin >> age;
cout << "You are " << age << " years old, " << fullName << endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.