3 Add an exit condition to Poe following code to prevent from become an infinite
ID: 3563279 • Letter: 3
Question
3 Add an exit condition to Poe following code to prevent from become an infinite loop. ( Hint: while(1) is always true. So it goes into an infinite loop. ) do{ // Menu cout ? Menu ; cout ? 1. Area of circle ; cout ? 2. Area of rectangle ; cout ? Enter your choice: ; cin ? choice; cout ? You have chosen ; // implement all the choices switch (choice) case 1: // area areaCircle = 3.14xrxr, break; case 2: // area areaRect= length* width; break; default: cout ? Invalid entry ; cout ? Try again ; cout ? end|; break; } // close switch case } while (1);Explanation / Answer
do{
//menu
cout << "Menu ";
cout << "1. Area of circle ";
cout << "2. Area of rectangle ";
cout << "3. Exit the program "; // giving user an extra option to exit
cout << "Enter your choice ";
cin >> choice;
cout << "you have chosen the choice number " << choice << endl;
switch(choice) {
case 1: cout << "Area of circle is " << (3.14*r*r) << endl;goto label; break; /*displaying the result, here break is not necessary as control directly jumps to label: thus exiting the loop*/
case 2: cout << "Area of rectangle is " << (length*width) << endl;goto label; break; /*same is the case here as above*/
case 3: cout << "Exiting loop" << endl; goto label; break; /* this is the case if user doesnot want to calculate either case 1 or case 2 */
default: cout << "Invalid entry try again ";break;
}while(1);
label: // the control jumps here after the execution of either ooption 1 or 2 or 3
// HOPE THE ANSWER SATISFIES YOU
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.