In: Computer Science
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
Notes and Hints:
1) You MUST use a DO-WHILE LOOP
2) You must accept the upper and lower case versions of Y and N in the user's reply
3) Hint: That function you used in the previous problem with have a small issue here. That is because there is a newline (Enter) in the keyboard buffer after the user types Y or N. Do you remember how we ignored that extra newline in Chp 3?
Starter Code:
// VARIABLES
// INPUT AND LOOP
std::cout << "Enter the full name of a person that can
serve as a reference: ";
std::cout << std::endl; // For Mimir
std::cout << name << " is reference #" <<
YOUR_CODE << std::endl;
std::cout << "Would you like to enter another name (Y or N)?
";
std::cout << std::endl; // For Mimir
// OUTPUT FOR COUNTER
std::cout << std::endl; // So that final line looks
nice
std::cout << "You have a total of " <
Previous pogram:
#include<iostream>
using namespace std;
int main()
{
string str;
int refcount=0;
cout<<"Enter the full name of a person that can serve as a
reference (type 'exit' to quit): "<<endl;
getline(cin,str);
if(str.compare("exit")==0)
{
refcount=0;
}
else
{
while(str.compare("exit")!=0)
{
refcount++;
cout<<str<<" is reference
#"<<refcount<<endl;
cout<< "Enter the name of another person that can serve as a
reference (type 'exit' to quit): "<<endl;
getline(cin,str);
}
}
cout<<"\n\n"<<"You have a total of
"<<refcount<<" references\n"<< endl;
return 0;
}
** Compiled in Dev cpp**
CODE:
#include<iostream>
using namespace std;
int main()
{
string str;
char choice;
int refcount=0;
/*
Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop checks its condition at the bottom of the loop.
A do...while loop is similar to a while loop,
except the fact that it is guaranteed to execute at least one time.
*/
do
{
cout << "Enter the full name of a person that can serve as a reference: " << endl;
// cin.ignore() is used to ignore the extra newline.
cin.ignore();
// reading the name using getline().
getline(cin, str);
// incrementing the refcount.
refcount++;
cout << str << " is reference #" << refcount << endl;
cout << "Would you like to enter another name (Y or N)? " << endl;
// reading the choice.
cin >> choice;
}
// This loop repeats if the choice is either Y or y.
while(choice == 'Y' || choice == 'y');
cout<< "\n\n" << "You have a total of " << refcount << " references\n" << endl;
return 0;
}
Screenshots:
refer this to know the proper indentations.
Sample I/O: