Question

In: Computer Science

Modify the previous program to use the Do-While Loop instead of the While Loop. This version...

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;
}

Solutions

Expert Solution

** 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:


Related Solutions

Do the following lab by while or Do-while loop. question: Write a c++ program that asks...
Do the following lab by while or Do-while loop. question: Write a c++ program that asks students to enter 3 valid grades and then calculate the average and print it. if the user enters the invalid grade you should print the error message and get the new grade. Hint1: each time your program ask the following question: "Do you want to calculate another average?" and if the answer to this question is "Y" or "y" then you should continue. Hint2:...
Problem: Construct a C program that will make use of user-defined functions, the do/while loop, the...
Problem: Construct a C program that will make use of user-defined functions, the do/while loop, the for loop, and selection. Using a do/while loop, your program will ask/prompt the user to enter in a positive value representing the number of values they wish to have processed by the program or a value to quit/exit. If the user enters a 0 or a negative number the program should exit with a message to the user indicating they chose to exit. If...
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers....
Java program Use Do-while Write a do-wile loop that asks the user to enter two numbers. The numbers should be added and the sum displayed. The loop should ask the user whether he or she wishes to perform the operation again. If so, the loop should repeat, otherwise, it should terminate. Use Continue branching statement Write a program that reads an integer and display all the positive ODD numbers from 0 to n (integer entered by the user). Use CONTINUE...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your...
This is for C++ You must use either a FOR, WHILE, or DO-WHILE loop in your solution for this problem. Write a quick main console program to output the following checker pattern to the console: #_#_#_#_# _#_#_#_#_ #_#_#_#_# _#_#_#_#_ #_#_#_#_#
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
write a program bus management system? using: if else, for loop, do while loop, function, arrays,...
write a program bus management system? using: if else, for loop, do while loop, function, arrays, string, structure
Re-write following while loop into Java statements that use a Do-while loop. Your final code should...
Re-write following while loop into Java statements that use a Do-while loop. Your final code should result in the same output as the original code below. int total = 0; while(total<100) { System.out.println("you can still buy for"+(100-total)+"Dollars"); total=total+5; }
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to...
Write a program in C++ coding that utilizes a DO-WHILE loop to redisplay a menu to the user. Ask the user to input their favorite SuperHero and display a positive or funny comment about the chosen SuperHero. If RBG is chosen, display "You can't spell TRUTH without RUTH." Utilize a Do-While Loop to redisplay the menu to the user after item 1, 2 or 3 is executed. If item 4 is chosen, end the program. If the user chooses 4,...
Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT