【问题标题】:Switch menu calculator will not display arithmetic切换菜单计算器不会显示算术
【发布时间】:2015-04-12 05:26:25
【问题描述】:

对不起,如果标题令人困惑,我不知道如何很好地表达我的问题。 我知道我的一些程序现在还没有完成,我目前只处理键盘输入部分。

基本上,我必须创建一个程序,允许用户输入带有集合整数的文件或让用户输入他们自己的两个整数。该用户还被询问他们希望对其整数执行哪种算术运算。我使用 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);
}

【问题讨论】:

    标签: c++ menu switch-statement


    【解决方案1】:
    cout << "The total is: " + digitTotal;
    

    您不能简单地将数字转换为字符串并使用 + 运算符将其连接起来。

    编辑: 你可以这样做:

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

    these

    【讨论】:

    • 或者最常见的版本:cout &lt;&lt; "The total is: " &lt;&lt; digitTotal;。无需转换。
    猜你喜欢
    • 1970-01-01
    • 2014-09-23
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    相关资源
    最近更新 更多