Xcode C++程序结果未显示正确答案
Xcode C++ Program result not displaying correct answer
我这里有一个程序,它使用两个随机生成的数字随机运行加法或减法。然后,用户必须输入答案,如果正确,则说"正确",如果错误,则说"不正确"并显示正确答案。当我运行代码时,它似乎没有显示正确的答案,而是它认为正确的错误答案。例如,我运行代码,我遇到的问题是 950 + 921。答案应该是1871,但它告诉我我错了,程序的正确答案是1042。我不确定错误在我的代码中的位置,但如果你想看看,这里是:
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(NULL)));
cout << "Welcome to the Math Tutor!" << endl;
int N1 = rand() % 999;
int N2 = rand() % 100;
int symbol = rand() % 2;
int Result;
int Answer;
cout << setw(5) << N1 << endl;
if(symbol == 1)
{
cout << "+";
Result = N1 + N2;
}
else
{
cout << "-";
Result = N1 - N2;
}
cout << setw(3) << N2 << symbol << "n";
cout << setw(5) << "------nn";
cout << "Enter your answer: ";
cin >> Answer;
if(Answer == Result)
{
cout << "You are correct!nn";
}
else
{
cout << "You are incorrect, the correct answer is: " << Result << "nn";
}
cin.ignore(1);
return 0;
}
你在 N2
之后输出symbol
,从这一行:
cout << setw(3) << N2 << symbol << "n";
因此,实际计算不是950 + 921
而是950 + 92
1042
。
要解决此问题,请不要输出symbol
,如下所示:
cout << setw(3) << N2 << "n";
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 我的程序将 26 个字母转换为 ascII 没有显示正确答案
- 任何人都可以弄清楚这段代码如何显示运行错误?它打印无限时间 -1 以及正确答案
- 如何在 WxWidgets 中显示方程式的答案?
- 删除矢量显示代码 c++ 段时答案更改
- 霍纳斯定律需要代数帮助,显示错误的答案 c++
- 为什么答案不会显示
- 当总和本身显示为180时,当我从两个角度的总和中提取180时,我会得到错误的答案
- 牛顿方法找到立方根,答案每次都显示为0
- 以C++显示时带有单引号和双引号的不同答案
- C++测验:使用矢量正确循环显示答案选项
- 如何使按钮显示随机答案
- C++构造函数并使其显示默认答案
- 为什么此代码不显示计算的正确答案,为什么它会无限循环?
- C++字符串。为什么答案显示字符串"狗"大于"猫",然后"猫"大于"狗"?
- 如何在程序中显示cin答案
- 使用类,但无法显示答案。
- 如何在c++中显示超过1小数点长的答案
- 阶乘在main中显示错误的答案.在函数中正确计算
- Xcode C++程序结果未显示正确答案