Help debugging a c++ program Hello, I have this section of code and it is not wo
ID: 3883808 • Letter: H
Question
Help debugging a c++ program
Hello, I have this section of code and it is not working properly and need help fixing it
case 5: {
cout << "How many users? ";
cin >> amt;
cout << "Enter users:" << endl;
//I want to grab a name for each of the users that
//the user gives me and output it with "Contacting <username>"
for (int i = 0;i <= amt;i++); {
string name;
cin >> name;
cout << "Contacting " << name << endl;
}
break;
}
What is currently outputting is:
How many users? Enter users:
Contacting John
However what should be outputting is:
How many users? Enter users:
Contacting John
Contacting Caleb
Contacting Jacob
please help
Explanation / Answer
There is a buffer thats keeping the residue of the string input from the user , use getline() to just take the input from the user.
case 5: {
cout << "How many users? ";
cin >> amt;
cout << "Enter users:" << endl;
//I want to grab a name for each of the users that
//the user gives me and output it with "Contacting <username>"
for (int i = 0;i <= amt;i++); {
string name;
getline(cin, name); // changed cin to getline
cout << "Contacting " << name << endl;
}
break;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.