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 }
/*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;
}
Comments
Leave a comment