我做了一个简单的程序来加减给定的数字。假设给出的 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/