Modify the previous program to use the Do-While Loop instead of the While Loop. This version of the program will ask the user if they wish to enter another name and accept a Y or N answer. Remove the "exit" requirement from before.
Output:
Enter the full name of a person that can serve as a reference: [user types: Bob Smith]
Bob Smith is reference #1
Would you like to enter another name (Y or N)? [user types: y]
Enter the full name of a person that can serve as a reference: [user types: Maria Garcia]
Maria Garcia is reference #2
Would you like to enter another name (Y or N)? [user types: N]
You have a total of 2 references
#include <iostream>
#include <string>
using namespace std;
int main() {
string fullName;
string answer;
int referenceNumber=1;
do{
cout<<"Enter the full name of a person that can serve as a reference: ";
getline(cin,fullName);
cout<<fullName<<" is reference #"<<referenceNumber<<"\n";
referenceNumber++;
cout<<"Would you like to enter another name (Y or N)? ";
getline(cin,answer);
}while(answer.compare("y")==0 || answer.compare("Y")==0);
referenceNumber--;
cout<<"You have a total of "<<referenceNumber<<" references\n";
system("pause");
return 0;
}
Comments
Leave a comment