'How do I loop a menu driven program that uses a switch case?
I need to ask the user in this way "Do you want to do it again?" and the user will choose Y or N whether they want to compute another set of numbers and go through the program all over again.
#include <iostream>
using namespace std;
int main()
{
float x, y;
float Sum, Difference, Product, Quotient;
char choice;
cout << "Enter x and y: ";
cin >> x >> y;
cout << "MENU" << endl;
cout << "A: Addition " << endl;
cout << "B: Subtraction" << endl;
cout << "C: Multiplication " << endl;
cout << "D: Division " << endl;
cout << "E: Exit " << endl;
cout << "Enter your choice :";
cin >> choice;
switch (choice)
{
case 'A':
Sum = x+y;
cout << x << "+" << y << "=" << Sum <<endl;
break;
case 'B':
Difference = x-y;
cout << x << "-" << y << "=" << Difference <<endl;
break;
case 'C':
Product = x*y;
cout << x << "*" << y << "=" << Product <<endl;
break;
case 'D':
Quotient = x/y;
cout << x << "/" << y << "=" << Quotient <<endl;
break;
case 'E':
break;
default:
cout << "Invalid input" << endl;
}
return 0;
}
Solution 1:[1]
It's actually pretty simple. You can use loops as such:
#include <iostream>
using namespace std;
void showChoices();
int main()
{
while (true) // This is a while loop
{
system("cls"); // Clear the screen. If you don't want to clear the screen you can remove this line
float x, y;
float Sum, Difference, Product, Quotient;
char choice;
cout << "Enter x and y: ";
cin >> x >> y;
showChoices();
cin >> choice;
switch (choice)
{
case 'A':
Sum = x + y;
cout << x << "+" << y << "=" << Sum << endl;
break;
case 'B':
Difference = x - y;
cout << x << "-" << y << "=" << Difference << endl;
break;
case 'C':
Product = x - y;
cout << x << "*" << y << "=" << Product << endl;
break;
case 'D':
Quotient = x - y;
cout << x << "/" << y << "=" << Quotient << endl;
break;
case 'E':
return 0; // Instead of break, use return here
default:
cout << "Invalid input" << endl;
}
}
}
void showChoices()
{
cout << "MENU" << endl;
cout << "A: Addition " << endl;
cout << "B: Subtraction" << endl;
cout << "C: Multiplication " << endl;
cout << "D: Division " << endl;
cout << "E: Exit " << endl;
cout << "Enter your choice :";
}
Also consider removing the line:
using namespace std
...because it's not considered as a good practice.
Also consider refactoring your code by moving the code inside the loop to a separate function, which returns a boolean something like this:
#include <iostream>
using namespace std;
void showChoices();
bool askUser();
int main()
{
while (askUser()) { /* Loop */ }
}
void showChoices()
{
cout << "MENU" << endl;
cout << "A: Addition " << endl;
cout << "B: Subtraction" << endl;
cout << "C: Multiplication " << endl;
cout << "D: Division " << endl;
cout << "E: Exit " << endl;
cout << "Enter your choice :";
}
bool askUser()
{
system("cls"); // Clear the screen. Can be removed if you don't want to do so.
float x, y;
float Sum, Difference, Product, Quotient;
char choice;
cout << "Enter x and y: ";
cin >> x >> y;
showChoices();
cin >> choice;
switch (choice)
{
case 'A':
Sum = x + y;
cout << x << "+" << y << "=" << Sum << endl;
break;
case 'B':
Difference = x - y;
cout << x << "-" << y << "=" << Difference << endl;
break;
case 'C':
Product = x - y;
cout << x << "*" << y << "=" << Product << endl;
break;
case 'D':
Quotient = x - y;
cout << x << "/" << y << "=" << Quotient << endl;
break;
case 'E':
return false;
default:
cout << "Invalid input" << endl;
}
return true;
}
I hope it was useful :).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
