Basic Cout problem c++

I've been programming this simple and in my opinion fun little random number guessing game, but i've gotten a small but annoying problem.
when i launch the program it works as planed all the way until it prints the number that it generated. When the number is printed example. 1 it will also print 10 for some wierd reason.
Does anyone have a clue?

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
 /*
	Guess the number;
	By
	Me
*/




//Library
#include <iostream>
#include <random>
#include <ctime>
#include <string>


using namespace std;



//Program starts here!

int main()
{

	int MAX_MISTAKES = 7;
	int mistakes = 0;
	int guess;

	


	while (true)
	{
		//random number generator iniciator 
		default_random_engine randomGen(time(0));
		uniform_int_distribution<int>randomNum(1, 10);

		//intro
		cout << "Guess a number between 1-10\n>> ";
		cin >>guess;
		cin.ignore();
		mistakes++;

		//guess right
		if (guess == randomNum(randomGen))
		{
			system("cls");
			cout << "Good job, you guessed right.\nThe number was " << randomNum << endl;
			cin.ignore();
			break;

		}

		//guess wrong to many times (7)
		if (mistakes == MAX_MISTAKES)
		{
			system("cls");
			cout << "Tobad, you lost!\n" << "The word was " << randomNum << endl;
			cin.ignore();
			break;
		}
	}
}


Thanks for all the help i can get.
I have fixed your code, the problem was that you kept on generating random numbers every time the user guessed so it would have been different every time. You needed to generate the random number and then store that into a variable which the guess could then be compared to.

You'll notice there are now 2 loops, the first one resets the mistakes variable and then generates a random number, the second does the same as before but looks slightly different and allows the user to continue guessing until they reached their limit.

Here's the code:
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
//Library
#include <iostream>
#include <random>
#include <ctime>
#include <string>

using namespace std;

//Program starts here!

int main()
{
    int MAX_MISTAKES = 7;
    int mistakes = 0;
    int guess;

    //random number generator iniciator
    default_random_engine randomGen(time(0));
    uniform_int_distribution<int>randomNum(1, 10);

    while (true)
    {
        mistakes = 0;
        int answer = randomNum(randomGen);

	while (mistakes != MAX_MISTAKES) {
	   //intro
           cout << "Guess a number between 1-10\n>> ";
	   cin >>guess;
	   cin.ignore();
	   mistakes++;

	   //guess right
	   if (guess == answer)
           {
	       system("cls");
	       cout << "Good job, you guessed right.\nThe number was " << answer << endl;
	       cin.ignore();
	       break;
            }
	    //guess wrong to many times (7)
	    else if (mistakes == MAX_MISTAKES)
	    {
	        system("cls");
		cout << "Tobad, you lost!\n" << "The word was " << answer << endl;
		cin.ignore();
	    }
        }
    }
}
Thanks alot, really bothered me. As you may have guessed i'm quite new to c++ and programming so something like that really helps. Thanks alot alot.
Topic archived. No new replies allowed.