切换菜单计算器不会显示算术运算

时间:2015-04-12 05:26:26

标签: c++ menu switch-statement

如果标题令人困惑,我很抱歉,我不知道如何很好地说出我的问题。 我知道我的一些程序目前尚未完成,我目前只处理键盘输入部分。

基本上,我必须创建一个程序,允许用户输入具有整数的文件或让用户输入自己的两个整数。该用户还被问到他们想要对其整数执行哪种算术。我使用switch语句创建了一个菜单和子菜单,使用户可以轻松导航到目的地。

我的问题是,当我尝试通过键盘选项使用输入时,我的程序无法显示计算的变量。我可以完全导航到该选项,甚至输入我的整数,但是当程序显示最终答案时,它会说:“总计是:菜单”,然后它将我踢回主菜单。

我的具体例子: 用户选择(2)键盘输入。 用户选择(1)进行加法运算 用户输入一个整数(1) 用户输入另一个整数(2) 程序显示“总计是:菜单” 程序循环回主菜单。

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

cout << "The total is: " + digitTotal;

你不能简单地将数字转换为字符串并使用+运算符连接它。

编辑: 你可以这样做:

cout << "The total is: " << itoa(digitTotal);

these