Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote