Answer to Question #234006 in C++ for BRIE

Question #234006

The following program has a bug when the user enters more than one name on the first

prompt. Execute the program by entering more than one string on the first prompt, observe

and state what is the bug on the program as a block comment, then modify the program to

fix the bug.

1 #include <iostream>

2 using namespace std;

3

4

5 int main(){

6

7 string name, surname;

8

9 cout<<"Please enter your name: ";

10 cin>>name;

11 cout<<"Please enter surname: ";

12 cin>>surname;

13 cout<<"Hi, "<<name<<" "<<surname<<endl;

14

15 return 0;

16 }


1
Expert's answer
2021-09-08T16:09:36-0400
/*The program stores the next any set of characters written after a white space in 
the input buffer so that when surname is requested, this buffered input is taken
instead of asking for fresh input*/
#include <iostream>


using namespace std;


int main(){




char name[20], surname[20];



cout<<"Please enter your name: ";
cin.getline(name, 20);


cout<<"Please enter surname: ";


cin.getline(surname, 20);
cout<<"Hi, "<<name<<" "<<surname<<endl;



return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS