My question is how to I get my program to return to the main menu after using ei
ID: 3566282 • Letter: M
Question
My question is how to I get my program to return to the main menu after using either option 1 or 2
program dev c++
program lanuage C+
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int option; // user's entered option will be saved in this variable
do //do-while loop starts here.that display menu again and again until user select to exit program
{
//Displaying Options for the menu
cout << "1) add " << endl;
cout << "2) multiply " << endl;
cout << "3) Exit Program " << endl;
//Prompting user to enter an option according to menu
cout << "Please select an option : ";
cin >> option; // taking option value as input and saving in variable "option"
if(option == 1) // Checking if user selected option 1
{
double a;
double b;
cout<<"Enter first number: ";
cin >> a;
cout <<"Enter the second number: ";
cin>> b;
cin.ignore();
double result = a + b;
cout<<"Result is"<<" "<<result<<endl;
cout << "press any key to continue " << endl;
}
else if(option == 2) // Checking if user selected option 2
{
double a;
double b;
cout<<"Enter first number: ";
cin >> a;
cout <<"Enter the second number: ";
cin>> b;
cin.ignore();
double result = a * b;
}
else if(option == 3) // Checking if user selected option 3
{
cout << "Terminating Program" << endl;
}
else //if user has entered invalid choice (other than 1,2,3)
{
//Displaying error message
cout << "Invalid Option entered" << endl;
}
}
while(option != 3); //condition of do-while loop
return 0;
}
b; cin.ignore(); double result = a + b; cout<<"Result is"<<" "<> a; cout <<"Enter the second number: "; cin>> b; cin.ignore(); double result = a * b; } else if(option == 3) // Checking if user selected option 3 { cout << "Terminating Program" << endl; } else //if user has entered invalid choice (other than 1,2,3) { //Displaying error message cout << "Invalid Option entered" << endl; } } while(option != 3); //condition of do-while loop return 0; }
Explanation / Answer
the code is compiling without errors, so I am explaining the code.
since it is do while loop, even if the while condition fails, the loop will execute once. hence for the first time, the loop runs and user enters option, based on the option. the particular option code executes.
now at the end, there is a while condition which checks if option is not to 3, if it is equal to 3. then the condition fails and the exists the while loop.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.