简体   繁体   English

C ++初学者-简单的输出

[英]C++ beginner - simple outputs

Here I have simple code to serve as a calculator for integers. 在这里,我有简单的代码可以用作整数计算器。

//Calculator, by Michael Lowry

#include <iostream>
using namespace std; 

void main ()
{
    int input = 0;
    int input2 = 0;
    int answer = 0;
    int operation = 0;

    cout << "Enter your first number" << endl;
    cin >> input;

    cout << "Type 1 for addition, 2 for subtraction, 3 for multiplication, or 4 for division" << endl;
    cin >> operation;

    cout << "Enter your second number" << endl;
    cin >> input2;

    if (operation = 1)
    {
        input + input2 == answer;
    }

    if (operation = 2)
    {
        input - input2 == answer;
    }

    if (operation = 3)
    {
        input * input2 == answer;
    }

    if (operation = 4)
    {
        input / input2 == answer;
    }

    cout << "Your answer is " <<
    cout << answer << endl;
    system ("PAUSE");
}

When I enter "1" for all three inputs, I get the output "Your answer is 6121DBCC0". 当我为所有三个输入都输入“ 1”时,将得到输出“您的答案是6121DBCC0”。 Why is my answer variable all messed up? 为什么我的答案变量都搞砸了?

Your output goes wrong. 您的输出有问题。 You should have 你应该有

cout << "Your answer is " << answer << endl;

instead of 代替

cout << "Your answer is " << 
cout << answer << endl;

What happens is that you are writing the outstream object cout to output. 发生的情况是您正在将流出对象cout写入输出。

Also the comparison operators are wrong, as others have noted. 正如其他人指出的那样,比较运算符也是错误的。 You should have == instead of = in the if -statements and vice versa in the assignment part. if语句中应该有==而不是= ,反之亦然。 Like this: 像这样:

if (operation == 2)
{
    answer = input - input2;
}

There are several errors: this is assigning a value to the operation variable, not comparing it against something: 有几个错误:这是为operation变量分配一个值,而不是将它与某些值进行比较:

if (operation = 1)

it should rather be 应该是

if(operation == 1)

furthermore this doesn't assign the result of input+input2 to answer but rather makes an unused comparison evaluation 此外,这不会分配input+input2的结果来回答,而是进行未使用的比较评估

input + input2 == answer;

and it should rather be 它应该是

answer = input + input2;

You should change your code accordingly. 您应该相应地更改代码。 Finally this: 最后这个:

cout << "Your answer is " << 
cout << answer << endl;

is wrong since you're passing the cout object along (mind the operator<< ). 是错误的,因为要传递cout对象(请注意operator<< )。 That should have been 那应该是

cout << "Your answer is " << answer << endl;

Also: main() is supposed to return int . 另外: main()应该返回int

Thus your code should have looked like: 因此,您的代码应如下所示:

int main () {
    int input = 0;
    int input2 = 0;
    int answer = 0;
    int operation = 0;

    cout << "Enter your first number" << endl;
    cin >> input; 

    cout << "Type 1 for addition, 2 for subtraction, 3 for multiplication, or 4 for division" << endl;
    cin >> operation;

    cout << "Enter your second number" << endl; 
    cin >> input2;

    if (operation == 1) {
        answer = input + input2;
    }

    if (operation == 2) {
        answer = input - input2;
    }

    if (operation == 3) {
     answer = input * input2;
    }

    if (operation == 4) {
     answer = input / input2;
    }

    cout << "Your answer is " << answer << endl;
    system ("PAUSE");
}

Try it out 试试看

First: Use 第一:使用

if (operation == 1)

instead of 代替

if (operation = 1)

because == is for equality, = is for assignment. 因为==代表平等,所以=代表赋值。

Second: 第二:

answer = input1 + input2;

instead of 代替

input + input2 == answer;

Do this in all if statements. 在所有if语句中执行此操作。

Third: Use 第三:使用

cout << "Your answer is "  << answer << endl;

to print your answer. 打印您的答案。

And remember answer = input / input2 will give you integer division not floating point. 并记住answer = input / input2将为您提供整数除法而不是浮点数。

First mistake 第一个错误

If you want to assign value to variable answer you should do: 如果要为变量answer赋值,则应该执行以下操作:

answer = input1 (required operator here) input2;

In your code such construction: 在您的代码中,这样的构造:

input - input2 == answer;

Is wrong in 2 ways: 有两种错误的方式:

  1. If you want assign value to answer use assigning = not comparing == operator. 如果要分配值来回答,请使用分配=不比较==运算符。
  2. Assigning values goes from right to left, so your desired variable should be on the left side. 赋值从右到左,因此所需变量应位于左侧。

Second Mistake 第二次错误

In line if (val = yourConstant) you made very popular mistake - assigning inside if statement. if (val = yourConstant)您犯了一个非常普遍的错误-在if语句中赋值。 Many languages prohibit such things, because they are hard to detect without debuggining or tests. 许多语言禁止此类操作,因为如果不进行调试或测试就很难检测到它们。 The code inside if statement will be executed only if yourConstant will be more than 0. Instead please use if (val == yourConstant) or if (yourConstant == val) . 仅当yourConstant大于0时,才会执行if语句中的代码。相反,请使用if (val == yourConstant)if (yourConstant == val)

So instead of: 所以代替:

if (operation = 1)

do: 做:

if (operation == 1)

Also change your output to: cout << "Your answer is " << answer << endl; 还将您的输出更改为: cout << "Your answer is " << answer << endl;

Another mistake: 另一个错误:

input - input2 == answer;

This is wrong. 错了 You have to make answer equal to input - input2 . 您必须使答案等于input - input2 Also there should only be one = , because it is not a condition. 也应该只有一个= ,因为它不是条件。

So: 所以:

answer = input - input2;

Apply that to all your arithmetic inside the if statements. 将其应用于if语句中的所有算术。 That should fix it. 那应该解决它。 But try to not use system ("PAUSE"); 但是,请尽量不要使用system ("PAUSE"); because it has its problems if you transfer your code. 因为如果您转移代码,它就会出现问题。 Just a suggestion. 只是一个建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM