C++ 程序菜单使用做同时和切换
C++ Program menu using do while and switch
我能够向用户显示程序菜单,但我无法执行实际的数学运算。例如,当我输入 2 时,它只显示 0,而不是让我输入两个整数,然后将它们相乘或相加。如何让它允许用户输入 1、2 或 3 的选项,然后让它执行他们输入的操作?
#include <iostream>
using namespace std;
int main()
{
int choice;
int numberOne = 0;
int numberTwo = 0;
int sumOfTwoNumbers = 0;
int productOfTwoNumbers = 0;
do{
cout <<"Please select one of the following options: n";
cout << "1: Enter two integer valuesn"
"2: Add the two valuesn"
"3: Multiply the two valuesn"
"4: Exitn";
cout << "Enter your selection (1, 2,3 or 4): ";
std::cin >> choice;
switch (choice)
{
case 1:
cout << "Enter two integer values. " << endl;
cin >> numberOne >> numberTwo;
break;
case 2:
sumOfTwoNumbers = numberOne + numberTwo;
cout << sumOfTwoNumbers << endl;
break;
case 3:
productOfTwoNumbers = numberOne * numberTwo;
cout << productOfTwoNumbers << endl;
break;
case 4:
cout << "You have chosen Exit, Goodbye.";
break;
default:
cout<< "Your selection must be between 1 and 4!n";
break;
}
}while(choice!= '4');
return 0;
}
在情况 1 中,您只要求两个数字。在其他选项中,数字保留为其默认值0
。无论选择哪个选项,您都需要确保分配这两个数字。此外,您的情况没有多大意义,因为所有选项都需要输入两个数字。我删除案例 1 并简单地移动行
cout << "Enter two integer values. " << endl;
cin >> numberOne >> numberTwo;
在switch
语句上方:
cout <<"Please select one of the following options: n";
cout <<
"1: Add the two valuesn"
"2: Multiply the two valuesn"
"3: Exitn";
cout << "Enter your selection (1, 2, or 3): ";
std::cin >> choice;
cout << "Enter two integer values. " << endl;
cin >> numberOne >> numberTwo;
switch (choice)
{
case 1:
sumOfTwoNumbers = numberOne + numberTwo;
cout << sumOfTwoNumbers << endl;
break;
case 2:
//etc
相关文章:
- C++ 程序菜单使用做同时和切换
- 如何将我的程序添加到文件和文件夹的macOS右键单击菜单?
- 数组在返回时失去价值(库存/菜单程序)C++
- 试图调节我的 do-while 以停止在终端中退出程序.菜单不会根据需要循环使用当前语句
- 面向菜单的程序C++的困难
- 我的C++程序不接受菜单后的输入
- 带有C 的控制台应用程序菜单
- 参数和全局变量,菜单程序
- 将WM_CHANGEUISTATE发送到窗口应用程序后,当用鼠标访问菜单时,助记符不会显示在系统菜单上
- 基本菜单驱动程序C ,无限循环
- C++基本菜单驱动程序(调用函数)
- 对菜单程序使用多个 switch 语句
- 单击上下文菜单项时运行程序
- 应用程序菜单中显示错误
- 菜单驱动的程序,堆栈为带字符信息的链表
- 如何在不使用开关大小写的情况下用C++编写菜单驱动的程序
- 在Win32程序中C++,如何修改菜单的标题?
- 在C++中创建患者程序菜单的视觉建议
- 在顶部mac窗口上显示应用程序菜单
- 如何隐藏Qt小部件应用程序菜单栏