为什么我的输出显示了计算器中的每个选项?

时间:2015-04-27 14:01:05

标签: c++

我正在用C ++(Visual Studio)做一个计算器,让你选择你想要的操作(加法,减法,乘法和除法)然后输入两个数字并得到答案。但是当我运行程序并在四个选项之间进行选择并输入两个数字时,程序会在每个选项中向我显示答案。这是代码,请告诉我我做错了什么,我该怎么做才能显示我做出的选择的答案。

#include <iostream>
using namespace std;

int main()
{

    int Q1Number, Num1, Num2, Answer;
    cout << "Welcome!" << endl;
    cout << "" << endl;
    cout << "Write the number to begin:" << endl;
    cout << " 1- Addition " << endl;
    cout << " 2- Subtraction " << endl;
    cout << " 3- Multiplication " << endl;
    cout << " 4- Division " << endl;
    cin >> Q1Number;
    cout << "Enter The first number";
    cin >> Num1;
    cout << "Enter The second number";
    cin >> Num2;



    if (Q1Number = 1)

        Answer = Num1 + Num2;

    cout << " Answer is : " << Answer << endl;

    if (Q1Number = 2);

        Answer = Num1 - Num2;

    cout << " Answer is : " << Answer << endl;

     if (Q1Number = 2);

        Answer = Num1 * Num2;

        cout << " Answer is : " << Answer << endl;  

    if (Q1Number = 2);

            Answer = Num1 / Num2;

        cout << " Answer is : " << Answer << endl;



}

4 个答案:

答案 0 :(得分:2)

您应该在=语句中将==强加给if
此外,如果你要分开,你应该检查Num2 != 0

#include <iostream>
using namespace std;

int main()
{

int Q1Number, Num1, Num2;
int Answer = 0;
cout << "Welcome!" << endl;
cout << "" << endl;
cout << "Write the number to begin:" << endl;
cout << " 1- Addition " << endl;
cout << " 2- Subtraction " << endl;
cout << " 3- Multiplication " << endl;
cout << " 4- Division " << endl;
cin >> Q1Number;
cout << "Enter The first number";
cin >> Num1;
cout << "Enter The second number";
cin >> Num2;
if (Num2 == 0 && Q1Number == 4)
    {
        cout << "Num2 is 0. We don't divide by zero" << endl;
        return 0;
    }

if (Q1Number == 1)
{
    Answer = Num1 + Num2;

    cout << " Answer is : " << Answer << endl;
}

if (Q1Number == 2)
{
    Answer = Num1 - Num2;

    cout << " Answer is : " << Answer << endl;
}

if (Q1Number == 3)
{
    Answer = Num1 * Num2;

    cout << " Answer is : " << Answer << endl;  
}

if (Q1Number == 4)
{
    Answer = Num1 / Num2;

    cout << " Answer is : " << Answer << endl;
}
return 0;

使用switch(Q1Number)代替使用if语句可能更好。
使用switch,它应该如下所示:

 #include <iostream>
    using namespace std;

    int main()
    {

    int Q1Number, Num1, Num2, Answer;
    cout << "Welcome!" << endl;
    cout << "" << endl;
    cout << "Write the number to begin:" << endl;
    cout << " 1- Addition " << endl;
    cout << " 2- Subtraction " << endl;
    cout << " 3- Multiplication " << endl;
    cout << " 4- Division " << endl;
    cin >> Q1Number;
    cout << "Enter The first number";
    cin >> Num1;
    cout << "Enter The second number";
    cin >> Num2;

    if (Num2 == 0 && Q1Number == 4)
    {
        cout << "Num2 is 0. We don't divide by zero" << endl;
        return 0;
    }
    switch(Q1Number)
    {
        case 1:
            Answer = Num1 + Num2;
            break;
        case 2:
            Answer = Num1 - Num2;
            break;
        case 3:
            Answer = Num1*Num2;
            break;
        case 4:
            Answer = Num1/Num2;
            break;
        default: 
            cout << " Wrong option selected " << endl
            break;
    }

    cout << " Answer is : " << Answer << endl

    return 0;
}

答案 1 :(得分:1)

问题出在你的if陈述中。您需要使用==代替=。使用=,您正在分配值,这就是问题所在。

所以,改变

if (Q1Number = 1)

if (Q1Number == 1)

然后对所有if语句进行相同的编辑。

当它为=时,运算符返回它指定的值,因此在您的情况下,分配的值为1和2,结果为true,因此if中的条件声明得到满足。

答案 2 :(得分:1)

您实际上是在分配而不是测试(operator =与operator ==不同)

答案 3 :(得分:1)

if(a = b)表示将b分配给a,然后测试结果是否评估为true。只要b非零,就是这种情况。要测试是否相等,您需要使用==运算符if(a == b)