c++ - 加法和减法给出不正确的结果

标签 c++

我做了一个简单的程序来加减给定的数字。假设给出的 2 个数字是 5 和 5。它会打印 5 + 5 = 10 和 5 - 5 = 0。现在我不确定哪里出了问题。我可能需要一个临时变量,但输入的东西不对。如果你用 5 和 5 测试数字,它会打印:

Addition / Subtraction Program
*------------------------------*

Press Enter to begin!
What is the number you'd like to add / sub to?5
5
What is the next number?5
55 + 5 = 105 - 5 = 0

这是我使用的代码:

#include <iostream>

using namespace std;

int main() {

    int num_1;
    int num_2;

    cout << "Addition / Subtraction Program" << endl << "*------------------------------*\n\nPress Enter to begin!";
    cin.get();

    cout << "What is the number you'd like to add / sub to?";

    cin >> num_1;

    cout << num_1 << endl << "What is the next number?";

    cin >> num_2;

    cout << num_2;

    cout << num_1 << " + " << num_2 << " = " << num_1 + num_2;

    cout << num_1 << " - " << num_2 << " = " << num_1 - num_2;
    return 0;
}

最佳答案

输出是正确的,只是缺少空格。

用户输入他们的电话号码后,您将其回显给他们;没有任何空格。这会将 5 变成 55,将 10 5 变成 105。

加法和减法都可以,你只需要格式化你的输出。结束你的cout带有 << endl; 的行或 << "\n";看看区别。

关于c++ - 加法和减法给出不正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30010728/

相关文章:

c++ - 指针和多维数组

c++ - 获取触摸在 ScrollView 中的位置

c# - 引用同一 dll 的非托管代码的包装器的多个实例

c++ - 在 ARM 上混合使用 uint64_t 和 long 会产生奇怪的结果

c++ - 从类节点上的结构中创建二叉搜索树会很糟糕吗?

c++ - 如何将删除器传递给 shared_ptr 持有的同一类中的方法

c++ - 将数字转换为文本,C++

c++ - 参数中的函数指针

c++ - 取消屏蔽 `poll` ?

c++ - while 循环不等待方法完成