如何循环使用开关盒的菜单驱动程序



我需要以这种方式询问用户"你想再做一次吗"并且用户将选择Y或N,无论他们是否想要计算另一组数字并重新执行该程序。

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

其实很简单。你可以使用这样的循环:

#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 :";
}

同时考虑删除线路:

using namespace std

因为这不是一个好的做法。

还可以考虑通过将循环中的代码移动到一个单独的函数来重构代码,该函数返回如下的布尔值:

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

我希望它有用:(。

最新更新