C++ C++;使用do while和switch对菜单进行编程
我能够向用户显示程序菜单,但我无法执行实际的数学运算。例如,当我输入2时,它只显示0,而不是让我输入两个整数,然后将它们相乘或相加。我如何才能让它允许用户输入1、2或3的选项,然后让它执行他们输入的操作C++ C++;使用do while和switch对菜单进行编程,c++,C++,我能够向用户显示程序菜单,但我无法执行实际的数学运算。例如,当我输入2时,它只显示0,而不是让我输入两个整数,然后将它们相乘或相加。我如何才能让它允许用户输入1、2或3的选项,然后让它执行他们输入的操作 #include <iostream> using namespace std; int main() { int choice; int numberOne = 0; int numberTwo = 0; int sumOfTwo
#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 values\n"
"2: Add the two values\n"
"3: Multiply the two values\n"
"4: Exit\n";
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;
}
#包括
使用名称空间std;
int main()
{
智力选择;
int numberOne=0;
整数wo=0;
int sumOfTwoNumbers=0;
int productOfTwoNumbers=0;
做{
库特数2;
打破
案例2:
sumOfTwoNumbers=numberOne+numberTwo;
不能在案例1中只要求输入两个数字。在其他选项中,数字保留为默认值0
。无论选择哪个选项,您都需要确保分配两个数字。此外,您的案例没有多大意义,因为所有选项都需要输入两个数字。我删除案例1,只需移动e线
cout << "Enter two integer values. " << endl;
cin >> numberOne >> numberTwo;
cout <<"Please select one of the following options: \n";
cout <<
"1: Add the two values\n"
"2: Multiply the two values\n"
"3: Exit\n";
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