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?
/*
Guess the number;
By
Me
*/
//Library
#include <iostream>
#include <random>
#include <ctime>
#include <string>
usingnamespace 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;
}
}
}
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.