//main program
int main()
{
//Data
srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
string objective = "play the overUnder game."; //program objective
string instructions = "I'm thinking of a number between 1 and 100. Guess what it is to win the game"; //user instructions
int userGuess; //users guess
int compGuess = rand() % 100; //random number
//user introduction
introduction(objective, instructions);
do
{
//user input
cout << "What is your guess? " << endl;
cin >> userGuess;
cin.ignore(1000, 10);
if (userGuess == compGuess)
{
cout <<"Thats right -- it's " << compGuess << endl;
break;
}
elseif (userGuess > compGuess)
{
cout <<"Thats too high -- guess again " << endl;
}
elseif (userGuess < compGuess)
{
cout <<"Thats too low -- guess again " << endl;
}
else
cout <<"Invalid -- guess again " << endl;
}while (userGuess != compGuess);
srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
string objective = "play the overUnder game."; //program objective
string instructions = "I'm thinking of a number between 1 and 100. Guess what it is to win the game"; //user instructions
int gameLog[SIZE];
int game_count = 0;
int userGuess; //users guess
int compGuess = rand() % 10; //random number
//user introduction
introduction(objective, instructions);
while (userGuess != compGuess)
{
//user input
cout << "What is your guess? " << endl;
cin >> userGuess;
cin.ignore(1000, 10);
gameLog[game_count++] = userGuess; //
//for loop (did user already use that number)
for (int u = 0; u < SIZE; u++)
{
if (userGuess == gameLog[u])
cout <<"Already entered that number. " << endl;
break;
} //for
//
if (userGuess == compGuess)
{
cout <<"Thats right -- it's " << compGuess << endl;
break;
}
elseif (userGuess > compGuess)
{
cout <<"Thats too high -- guess again " << endl;
}
elseif (userGuess < compGuess)
{
cout <<"Thats too low -- guess again " << endl;
}
else
{
cout <<"Invalid -- guess again " << endl;
}
}
I am just saying to make an array that contains a representative of each possible value, from 1 to 100 but because its indexed from 0 you need 101, see? Now you have one array location for each possible value, all set to false to begin with. as the user puts in values, you set the location to true, so you have seen that one before. They put it in again, you trapped it, you see that you already have used it and can do whatever.
your idea will work too, it is not wrong, but you don't need to loop over a giant list of inputs, you can check it in a single not-looped statement -- is the represented value true or false is all you need to know. Tracking it your way is more work all the way around.
also you have at least one bug:
1 2 3 4 5 6 7 8
for (int u = 0; u < SIZE; u++)
{
if (userGuess == gameLog[u])
cout <<"Already entered that number. " << endl;
break; //this happens no matter what, ending the loop after 0 is checked, regardless
//this break is NOT inside the IF statement!
} //for
srand(time(0)); //or else the sequence of random numbers will be the same every time your program runs
string objective = "play the overUnder game."; //program objective
string instructions = "I'm thinking of a number between 1 and 100. Guess what it is to win the game"; //user instructions
int gameLog[SIZE];
int userGuess; //users guess
int nmbSave; //index saver
int compGuess = rand() % 100; //random number
bool found = true;
//user introduction
introduction(objective, instructions);
do
{
//user input
cout << "What is your guess? " << endl;
cin >> userGuess;
cin.ignore(1000, 10);
//for loop (did user already use that number)
for (int u = 0; u < SIZE; u++)
{
if (userGuess == gameLog[u])
{
cout <<"You already entered " << gameLog[u] << "-- Try again " << endl;
found = true;
nmbSave = u;
}
else
{
found = false;
}
}
//
if (userGuess == compGuess)
{
cout <<"Thats right -- it's " << compGuess << endl;
found = true;
}
elseif (userGuess > compGuess)
{
gameLog[nmbSave] = userGuess;
cout <<"Thats too high -- guess again " << endl;
found = true;
}
elseif (userGuess < compGuess)
{
gameLog[nmbSave] = userGuess;
cout <<"Thats too low -- guess again " << endl;
found = true;
}
}while (userGuess != compGuess);
}//main
it keeps saying I've entered 2 & 6 before after my first time entering them. It also still shows if its too high or too low even if I have entered it before, how can I stop that?
Make it be bool gameLog[100] = {}; like how jonnin said.
Then you check it like
1 2 3 4 5 6 7 8 9
if (gameLog[userGuess])
{
// Already entered
}
else
{
gameLog[userGuess] = true;
// Handle new guess ...
}
Alternatively, keep it the way you have it, but don't set found = false on line 31. Set found to false on line 19, and then only after set it to true within the for loop. If, after the for loop is over, it is still set to false, that means it's a new guess.