要努力变更好 2022-07-04 15:50 采纳率: 100%
浏览 87
已结题

猜数字双线程实现C++

问题遇到的现象和发生背景

编译器报错

问题相关代码,请勿粘贴截图
#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;
}

运行结果及报错内容

img

我的解答思路和尝试过的方法
我想要达到的结果

用一个线程控制时间,一个线程用于猜数,当时间终止,猜数进程也被迫结束

  • 写回答

3条回答 默认 最新

  • flower980323 2022-07-04 18:05
    关注
    
    #include<iostream>
    #include<ctime>
    #include<thread>
    using namespace std;
    
    int answer;
    void init()
    {
        cout << endl;
        cout << "==========欢迎登陆小游戏=========" << endl;
        cout << "           祝您游戏愉快!" << endl;
        cout << "    ->1.开始游戏" << endl;
        cout << "    ->2.结束游戏" << endl;
    }
    bool findd = false;
    void Guess()
    {
        cout << "游戏规则猜一个数字(1-10000)" << endl;
        int x;
        while (1)
        {
            if (findd) break;
            cout << "请输入一个数字" << endl;
            cin >> x;
            if (x > answer)
            {
                cout << "高了" << endl;
            }
            else if (x < answer)
            {
                cout << "低了" << endl;
            }
            else
            {
                findd = true;
                cout << "恭喜您 猜对了 答案是:" << answer << endl;
                break;
            }
        }
        //exit(1);
    }
    void setTime()
    {
        bool tip = false;
        int Time = 30;
        int start = time(NULL);
        while (true) {
            if (findd) break;
            if (Time - (time(NULL) - start) <= 0)
            {
                printf("很遗憾游戏时间到,正确答案为%d\n", answer);
                findd = true;
                break;
            }
    
            if (Time - (time(NULL) - start) >= 10 && Time - (time(NULL) - start) <= 20 && !tip)
            {
                tip = true;
                cout << "友情提示:个位数字为:" << answer % 10 << endl;
            }
        }
    }
    int main()
    {
        srand((unsigned)time(0));
        answer= rand() % 10000 + 1;
        init();//游戏初始化
        int op; cin >> op;
        if (op == 1)
        {
            thread test01(setTime);
            thread test02(Guess);
            test01.join();
            test02.join();
        }
        else 
            exit(0);
        getchar();
        getchar();
        return 0;
    }
    

    如有帮助,请采纳,谢谢

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 7月14日
  • 已采纳回答 7月6日
  • 赞助了问题酬金5元 7月4日
  • 创建了问题 7月4日

悬赏问题

  • ¥15 为什么shp文件会有这种小方块?
  • ¥15 ecplise在连接数据库时显示加载驱动成功但是数据库连接失败
  • ¥15 visionmaster启动失败,提示为“机器不满足授权而被禁用”
  • ¥50 用logisim设计16位单时钟周期cpu
  • ¥15 IDEA中圈复杂度如何具体设置
  • ¥50 labview采集不了数据
  • ¥15 Multisim红外倒车雷达仿真中距离问题
  • ¥15 请上面代码做什么处理或什么混淆
  • ¥15 英雄联盟自定义房间置顶
  • ¥15 W5500网线插上无反应