【发布时间】:2016-07-13 16:38:26
【问题描述】:
我正在开发一个程序,该程序生成两个随机数和一个 if 语句,该语句生成“+”表示加法或“-”表示减法。我目前无法检查并查看我的 putput 是什么,因此我可以更正任何错误,因为程序运行我的开头“欢迎”语句,然后显示在蓝色括号 (lldb) 中并且代码停在那里。我注意到在我的 srand(time(0)) 函数旁边它变成绿色并显示“线程 1:断点 1.1”,并在其下方显示“隐式转换失去整数精度:'time_t'(又名'long')到'unsigned int '”。有没有办法解决这些问题或让错误消失?我的代码如下。任何帮助或见解将不胜感激,谢谢!
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main()
{
cout << "Welcome to the Math Tutor!" << endl;
int N1, N2;
int O = rand() % 2;
int Result;
int Answer;
srand(time(0));
if(O == 2)
{
cout << "+";
}
else
{
cout << "-";
}
N1 = 100 + rand() % 999;
N2 = 100 + rand() % 999;
Result = N1 + O + N2;
cout << setw(10) << N1 << endl;
cout << setw(10) << N2 << O << "\n";
cout << setw(10) << "------\n\n";
cout << "Enter your answer: ";
cin >> Answer;
if(Answer == Result)
{
cout << "You are correct!\n\n";
}
else
{
cout << "You are incorrect, the correct answer is: " << Result << "\n\n";
}
cin.ignore(1);
return 0;
}
【问题讨论】:
-
与您的问题无关,但
O的值将始终相同,因为您在初始化随机数生成器之前生成了它。编辑:您的代码似乎在ideone 上编译得很好。 -
请不要使用'O'作为变量名。它看起来太像“0”了。