猜数字双线程实现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

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

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


#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;
}

如有帮助,请采纳,谢谢

你这个报错原因是没给setTime传参,而且想开多线程setTime必须死循环


#include <chrono>
#include <iostream>
#include <thread>

using std::cin;
using std::cout;
using std::endl;
using std::thread;

int answer;

void init()
{
    cout << endl;
    cout << "==========欢迎登陆小游戏=========" << endl;
    cout << "           祝您游戏愉快!" << endl;
    cout << "    ->1.开始游戏" << endl;
    cout << "    ->2.结束游戏" << endl;
}

void Guess()
{
    printf("游戏规则猜一个数字(1-10000)\n");

    int x;

    while (true)
    {
        printf("请输入一个数字\n");
        scanf("%d", &x);

        if (x > answer)
        {
            printf("高了\n");
        }
        else if (x < answer)
        {
            printf("低了\n");
        }
        else
        {
            printf("\n恭喜您 猜对了 答案是: %d", answer);
            break;
        }
    }
    exit(1);
}

void setTime(int theTime = 30)
{
    if (theTime > 20)
    {
        std::this_thread::sleep_for(std::chrono::seconds(theTime - 20));
        printf("\n友情提示:个位数字为: %d\n", answer % 10);

        std::this_thread::sleep_for(std::chrono::seconds(20));
        printf("\n很遗憾游戏时间到,正确答案为%d\n", answer);

        exit(0);
    }
    else if (theTime <= 20 && theTime >= 10)
    {
        printf("\n友情提示:个位数字为: %d\n", answer % 10);
    }

    std::this_thread::sleep_for(std::chrono::seconds(theTime));
    printf("\n很遗憾游戏时间到,正确答案为%d\n", answer);

    exit(0);
}

auto main() -> int
{
#ifdef _WIN64
    system("chcp 65001");
#endif

    init(); //游戏初始化

    int op;
    cin >> op;

    if (op == 1)
    {
        srand(time(nullptr));
        answer = rand() % 10000 + 1;
        //     std::cout << answer << std::endl;

        thread test01(setTime, 30);
        thread test02(Guess);

        test01.join();
        test02.join();
    }

    return 0;
}