little problem on my code

Hello people and sorry for my english.
I have un problem on my little game console, found mysterious word.
When my count thread has finished, for me it should show me the response request but remain pending until the enter key is pressed.
if anyone has an idea I am more than interested because I believe I have been around the internet and nothing has been found to solve my problem.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
 #include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
#include <thread>
#include <chrono>
#include <unistd.h>
#include <iomanip>
#include <limits>

bool clavierEteint = false;

void compteurTemps(int nombreCoups, std::string motMelange)
{
    using namespace std::literals::chrono_literals;

    for (int i(3); i >= 0; i--)
    {
        std::system("clear");
        std::cout << "nombre restant d'essais : " << nombreCoups << "\r" << std::endl;
        std::cout << "\nQuel est ce mot : " << motMelange << "\r" << std::endl;
        std::cout << "temps restant : " << std::right << std::setw(2) << std::setfill('0') << i << "\r" << std::endl;
        std::this_thread::sleep_for(1s);
    }
    clavierEteint = true;
}

std::string melangerlettres(std::string mot)
{
    std::string melange;
    int position(0);

    while (mot.size() != 0)
    {
        position = rand() % mot.size();
        melange += mot[position];
        mot.erase(position, 1);
    }
    return melange;
}

int main()
{
    std::string motMystere, motMelange, motUtilisateur, reponseRejouer;
    srand(time(0));
    using namespace std::literals::chrono_literals;
    do
    {
        std::system("clear");

        std::cout << "mot mystere: " << std::endl;
        std::cin >> motMystere;

        std::system("clear");

        int nombreCoups = motMystere.size();

        motMelange = melangerlettres(motMystere);

        do
        {
            std::system("clear");

            std::cout << "nombre restant d'essais : " << nombreCoups << std::endl;
            std::cout << "\nQuel est ce mot : " << motMelange << std::endl;

            std::thread reponse(compteurTemps, nombreCoups, motMelange);

            while (!clavierEteint)
            {
                // std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
                std::cin.ignore();
            }

            std::system("clear");

            std::cout << "\nRéponse :  " << std::flush;
            reponse.join();

            std::cin >> motUtilisateur;

            nombreCoups -= 1;

            if (nombreCoups < 1)
            {
                std::cout << "COUPS DEPASSES." << std::endl;
                motUtilisateur = motMystere;
            }
            else
            {
                if (motUtilisateur != motMystere)
                {
                    std::cout << "NON RECOMMENCE." << std::endl;
                }
                else
                {
                    std::cout << "BRAVO." << std::endl;
                }
            }
            clavierEteint = false;
        } while (motUtilisateur != motMystere);

        std::cout << "\nVoulez-vous rejouer ? (o/n)" << std::endl;
        std::cin >> reponseRejouer;

    } while (reponseRejouer == "o" || reponseRejouer == "O");

    return 0;
}
Hello breton,

A note: srand(static_cast<unsigned int>(time(nullptr)));. This is the more up to date way of writing this. Although what you have will work

Line 23. I am not sure if that will work. I have always used
 
std::this_thread::sleep_for(std::chrono::seconds(1));

I will admit that I do not know everything about "chrono" and "thread" to know what can be used.

That said it would help if you could mention what line you believe is causing the problem, so I have a better idea what to look for.

Andy
hello and thank you.

loop "while" line 69 I guess must be the problem.
I'm not sure what are you trying to do on line 69? join() already waits for the end of the thread. So what's the loop good for?

It actually seems that the thread is unnecessary since you're not doing anything parallel.
I made a loop to prevent the player from typing on the keyboard and being taken as a bad answer from him. I couldn't see a way to count down and prevent wrong entry (I tried with a for but it supported keyboard input). I hope to have been clear. Anyway thank you for your answers.
It seems that you want to consume characters entered while clavierEtei ("isKeyboardOn") is false. You are trying to do this in the main thread like this:

1
2
while (!isKeyboardOn)
    std::cin.ignore();

The problem is that ignore() blocks and is also generally line-buffered.
On Windows you might try doing this with _kbhit() and _getch().
On *nix you might try doing it with simulations of those functions.
_kbhit() tells if a character is waiting in the input buffer.
_getch() reads (and removes) a character from the buffer.

So maybe something like:

1
2
3
    while (!isKeyboardOn)
        if (_kbhit())
            _getch();

Topic archived. No new replies allowed.