用户输入后程序关闭,没有理由应该

时间:2016-11-30 21:33:59

标签: c++ input

抱歉,我很新,不得不快点,所以我只是想把它说出来。在尝试第一个输入(选择什么主题,代数,基础数学等)后,代码随机结束。我尝试了其他来源,尝试重新格式化,但仍然不知道它是什么。我很新,所以我认为这只是一个我忽略的愚蠢问题。

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>

using namespace std;

int main() {
    int calculatorChoice, choiceMath1;
    int addition1, addition2, additionSum;
    int subtraction1, subtraction2, subtractionDifference;
    int multiplication1, multiplication2, multiplicationProduct;
    int division1, division2, divisionQuotient;
    cout << "What would you like to figure out?" << endl;
    cout << "" << endl;
    cout << "[1]: Basic Mathematic Equations" << endl;
    cout << "[2]: Figuring out a Grade" << endl;
    cout << "[3]: Algebra" << endl;
    cout << "[4]: Inquiry Sciences" << endl;
    cout << "[5]: Unit Conversion" << endl;
    cin >> calculatorChoice;
    if (calculatorChoice == 1) {
        cout << "What would you like to do?" << endl;
        cout << "" << endl;
        cout << "[1]: Addition" << endl;
        cout << "[2]: Subtraction" << endl;
        cout << "[3]: Multiplication" << endl;
        cout << "[4]: Division" << endl;
        cin >> choiceMath1;
        if (choiceMath1 == 1) {
            cout << "Choose your first number." << endl;
            cin >> addition1;
            cout << "Choose your second number." << endl;
            cin >> addition2;
            additionSum = addition1 + addition2;
            cout << "The sum of " << addition1 << " and " << addition2 << " is " << additionSum << "." << endl;

        }
        else if (choiceMath1 == 2) {
            cout << "Choose the first number." << endl;
            cin >> subtraction1;
            cout << "Choose the second number." << endl;
            cin >> subtraction2;
            subtractionDifference = subtraction1 - subtraction2;
            cout << "The difference of " << subtraction1 << " and " << subtraction2 << " is " << subtractionDifference << "." << endl;
        }
        else if(choiceMath1 == 3) {
            cout << "Choose the first number." << endl;
            cin >> multiplication1;
            cout << "Choose the second number." << endl;
            cin >> multiplication2;
            multiplicationProduct = multiplication1 * multiplication2;
            cout << "The product of " << multiplication1 << " and " << multiplication2 << " is " << multiplicationProduct << "." << endl;
        }
        else if(choiceMath1 == 4) {
            cout << "Choose the first number." << endl;
            cin >> division1;
            cout << "Choose the second number." << endl;
            cin >> division2;
            divisionQuotient = division1 / division2;
            cout << "The quotient of " << division1 << " by " << division2 << " is " << divisionQuotient << "." << endl;
        }
        else {
            cout << "That is not a choice." << endl;
        }
    }
    else {

    }
}
void calculator() {

}

1 个答案:

答案 0 :(得分:0)

您的代码没有任何循环,这意味着,在输入输入后,它会显示结果和程序完成。如果你想在某个地方停止你的代码(取决于IDE,这个解决方案适用于VS)只需键入system("pause"),你就会看到你的程序在控制台上显示的内容。如果您不在VS上,请添加#include <cstdlib>

如果你再解释一下,你希望你的代码可以帮助你更轻松。