问题遇到的现象和发生背景
编译器报错
问题相关代码,请勿粘贴截图
#include<iostream>
#include<ctime>
#include<thread>
using namespace std;
int answer = rand() % 10000 + 1;
void init()
{
cout << endl;
cout << "==========欢迎登陆小游戏=========" << endl;
cout << " 祝您游戏愉快!" << endl;
cout << " ->1.开始游戏" << endl;
cout << " ->2.结束游戏" << endl;
}
void Guess()
{
cout << "游戏规则猜一个数字(1-10000)" << endl;
int x;
while (1)
{
cout << "请输入一个数字" << endl;
cin >> x;
if (x > answer)
{
cout << "高了" << endl;
}
else if (x < answer)
{
cout << "低了" << endl;
}
else
{
cout << "恭喜您 猜对了 答案是:" << answer << endl;
break;
}
}
exit(1);
}
void setTime(int Time=30)
{
int start = time(NULL);
if (Time - (time(NULL) - start) <= 0)
{
printf("很遗憾游戏时间到,正确答案为%d\n", answer);
exit(0);
}
else if (Time - (time(NULL) - start) >= 10 && Time - (time(NULL) - start) <= 20)
{
cout << "友情提示:个位数字为:" << answer % 10 << endl;
}
}
int main()
{
init();//游戏初始化
int op; cin >> op;
if (op == 1)
{
thread test01(setTime);
thread test02(Guess);
test01.join();
test02.join();
}
else exit(0);
return 0;
}
运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
用一个线程控制时间,一个线程用于猜数,当时间终止,猜数进程也被迫结束