切换菜单计算器不会显示算术
Switch menu calculator will not display arithmetic
抱歉,如果标题令人困惑,我不知道如何很好地表达我的问题。
我知道我的一些程序现在还没有完成,我目前只处理键盘输入部分。
基本上,我必须创建一个程序,允许用户输入带有设置整数的文件或让用户输入他们自己的两个整数。还会询问该用户他们希望对他们的整数执行哪种算术。我使用 switch 语句创建了一个菜单和子菜单,允许用户轻松导航到他们的目的地。
我的问题是,当我尝试使用通过键盘输入选项时,我的程序无法显示计算变量。我可以完全导航到该选项,甚至可以输入我的整数,但是当程序显示最终答案时,它会显示:"The total is: menu" 然后它会让我回到主菜单。
我的具体例子:
用户选择 (2) 进行键盘输入。
用户选择 (1) 进行加法运算
用户输入一个整数 (1)
用户输入另一个整数 (2)
程序显示 "The total is: Menu"
程序循环回到主菜单。
这是我的代码:
#include "complx.h"
#include <iostream> using namespace std;
ifstream infile ("in.dat");
int main() {
int choiceOne, choiceOneSubMenu, choiceTwoSubMenu, digitOne, digitTwo, digitTotal;
bool menu = true;
do{
cout << "Menu \n";
cout << "=========== \n";
cout << "(1) Input from a file \n";
cout << "(2) Input from the keyboard \n";
cout << "(3) Exit the program \n";
cout << "Enter a numerical selection: \n";
cin >> choiceOne;
switch (choiceOne) {
case 1:
cout << "You chose input from a file \n";
cout << "=============== \n";
cout << "Which arithmetic would you like applied? \n";
cout << "(1) Addition + \n";
cout << "(2) Subtraction - \n";
cout << "(3) Multiplication * \n";
cout << "(4) Division / \n";
cout << "Enter a numerical selection: \n";
cin >> choiceOneSubMenu;
switch (choiceOneSubMenu) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
break;
case 2:
cout << "You chose input from the keyboard \n";
cout << "=============== \n";
cout << "Which arithmetic would you like applied? \n";
cout << "(1) Addition + \n";
cout << "(2) Subtraction - \n";
cout << "(3) Multiplication * \n";
cout << "(4) Division / \n";
cout << "Enter a numerical selection: \n";
cin >> choiceTwoSubMenu;
switch (choiceTwoSubMenu)
{
case 1:
cout << "You chose addition \n";
cout << "=============== \n";
cout << "Enter your first integer: \n";
cin >> digitOne;
cout << "Enter your second integer: \n";
cin >> digitTwo;
digitTotal = (digitOne + digitTwo);
cout << "The total is: " + digitTotal;
break;
case 2:
cout << "You chose subtraction \n";
cout << "=============== \n";
cout << "Enter your first integer: \n";
cin >> digitOne;
cout << "Enter your second integer: \n";
cin >> digitTwo;
digitTotal = (digitOne - digitTwo);
cout << "The total is: " + digitTotal;
break;
case 3:
cout << "You chose multiplication \n";
cout << "=============== \n";
cout << "Enter your first integer: \n";
cin >> digitOne;
cout << "Enter your second integer: \n";
cin >> digitTwo;
digitTotal = (digitOne * digitTwo);
cout << "The total is: " + digitTotal;
break;
case 4:
cout << "You chose division \n";
cout << "=============== \n";
cout << "Enter your first integer: \n";
cin >> digitOne;
cout << "Enter your second integer: \n";
cin >> digitTwo;
digitTotal = (digitOne / digitTwo);
cout << "The total is: " + digitTotal;
break;
}
break;
case 3:
cout << "You have chosen to exit";
}
}while(choiceOne!=3);
}
cout << "The total is: " + digitTotal;
您不能简单地将数字转换为字符串并使用 + 运算符连接它。
编辑:
你可以这样做:
cout << "The total is: " << itoa(digitTotal);
抱歉,如果标题令人困惑,我不知道如何很好地表达我的问题。 我知道我的一些程序现在还没有完成,我目前只处理键盘输入部分。
基本上,我必须创建一个程序,允许用户输入带有设置整数的文件或让用户输入他们自己的两个整数。还会询问该用户他们希望对他们的整数执行哪种算术。我使用 switch 语句创建了一个菜单和子菜单,允许用户轻松导航到他们的目的地。
我的问题是,当我尝试使用通过键盘输入选项时,我的程序无法显示计算变量。我可以完全导航到该选项,甚至可以输入我的整数,但是当程序显示最终答案时,它会显示:"The total is: menu" 然后它会让我回到主菜单。
我的具体例子: 用户选择 (2) 进行键盘输入。 用户选择 (1) 进行加法运算 用户输入一个整数 (1) 用户输入另一个整数 (2) 程序显示 "The total is: Menu" 程序循环回到主菜单。
这是我的代码:
#include "complx.h"
#include <iostream> using namespace std;
ifstream infile ("in.dat");
int main() {
int choiceOne, choiceOneSubMenu, choiceTwoSubMenu, digitOne, digitTwo, digitTotal;
bool menu = true;
do{
cout << "Menu \n";
cout << "=========== \n";
cout << "(1) Input from a file \n";
cout << "(2) Input from the keyboard \n";
cout << "(3) Exit the program \n";
cout << "Enter a numerical selection: \n";
cin >> choiceOne;
switch (choiceOne) {
case 1:
cout << "You chose input from a file \n";
cout << "=============== \n";
cout << "Which arithmetic would you like applied? \n";
cout << "(1) Addition + \n";
cout << "(2) Subtraction - \n";
cout << "(3) Multiplication * \n";
cout << "(4) Division / \n";
cout << "Enter a numerical selection: \n";
cin >> choiceOneSubMenu;
switch (choiceOneSubMenu) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
break;
case 2:
cout << "You chose input from the keyboard \n";
cout << "=============== \n";
cout << "Which arithmetic would you like applied? \n";
cout << "(1) Addition + \n";
cout << "(2) Subtraction - \n";
cout << "(3) Multiplication * \n";
cout << "(4) Division / \n";
cout << "Enter a numerical selection: \n";
cin >> choiceTwoSubMenu;
switch (choiceTwoSubMenu)
{
case 1:
cout << "You chose addition \n";
cout << "=============== \n";
cout << "Enter your first integer: \n";
cin >> digitOne;
cout << "Enter your second integer: \n";
cin >> digitTwo;
digitTotal = (digitOne + digitTwo);
cout << "The total is: " + digitTotal;
break;
case 2:
cout << "You chose subtraction \n";
cout << "=============== \n";
cout << "Enter your first integer: \n";
cin >> digitOne;
cout << "Enter your second integer: \n";
cin >> digitTwo;
digitTotal = (digitOne - digitTwo);
cout << "The total is: " + digitTotal;
break;
case 3:
cout << "You chose multiplication \n";
cout << "=============== \n";
cout << "Enter your first integer: \n";
cin >> digitOne;
cout << "Enter your second integer: \n";
cin >> digitTwo;
digitTotal = (digitOne * digitTwo);
cout << "The total is: " + digitTotal;
break;
case 4:
cout << "You chose division \n";
cout << "=============== \n";
cout << "Enter your first integer: \n";
cin >> digitOne;
cout << "Enter your second integer: \n";
cin >> digitTwo;
digitTotal = (digitOne / digitTwo);
cout << "The total is: " + digitTotal;
break;
}
break;
case 3:
cout << "You have chosen to exit";
}
}while(choiceOne!=3);
}
cout << "The total is: " + digitTotal;
您不能简单地将数字转换为字符串并使用 + 运算符连接它。
编辑: 你可以这样做:
cout << "The total is: " << itoa(digitTotal);