This is a guessing game in which the user has 10 tries to get the random number right. Whether the random number is guessed or not, the program will display the guesses the user entered.
I have everything perfect except for when you don't guess the number, the program displays the guesses, but the last guess somehow becomes the random number. Please help and take a look.
Please disregard comments!
/**************************** Compiler Directives *****************************/
#include <cstdlib>
#include <iostream>
#include <ctime>
usingnamespace std;
/************************** Global Data Declarations **************************/
//None in this program.
/**************************** Function Prototypes *****************************/
/*******************************************************************************
* Function Name: main
* Author: Chad Martinez
* Function Description:
*
* Pseudocode:
* Level 0
* ------- Variables
*
*******************************************************************************/
int main ()
{
//Local constants
constint SIZE = 10; //Sets the size of the array/chances
constint MIN = 1; //Change if lower range is needed
constint MAX = 100; //Change if higher range is needed
//Local variables
int count = 0; //Counter for array
int guess[SIZE]; //Array for having guesses stored into
int chances = 10; //Chances user has
char ans;
int tries = 0;
int count2 = 0;
int random; //Sets random number within range
/******************** Begin main Function Executables *************************/
do
{
//Creates Random number
srand(time(0));
random = (rand() % (MAX - MIN) + 1);
system("cls");
//Input guesses
cout << "I have a number between and including 1 and 100. You have " <<
chances << " to get it." << endl;
count = 0;
do
{
cout << "Guess #" << count + 1 << ": ";
cin >> guess[count];
tries++;
if ((guess[count] < MIN) || (guess[count] > MAX))
{
cout << "Please guess between " << MIN << " & " << MAX << " only."
<< endl;
}
elseif (guess[count] < random)
{
cout << guess[count] << " is low. Go higher." << endl;
count++;
}
elseif (guess[count] > random)
{
cout << guess[count] << " is high. Go lower." << endl;
count++;
}
}while(count != (SIZE - 1) && guess[count] != random);
if (guess[count] = random)
{
system("cls");
cout << "CONGRATULATIONS! The number I had was " << random
<< ". It took you " << count + 1 << " attempt(s) to get it.\n""Here were your guesses:" << endl;
for (count2 = 0; count2 <= count; count2 ++)
{
cout << "Guess #" << count2 + 1 << ": " << guess[count2]
<< endl <<endl;
}
}
elseif (count = (SIZE - 1))
{
cout << "Sorry, the number I had was " << random << ". Here were"
<< " your guesses:" << endl;
for (count = 0; count <= (SIZE - 1); count ++)
{
cout << "Guess #" << count + 1 << ": " << guess[count] << endl
<< endl;
}
}
//Successful Output
cout << "Try again? (Y/N): ";
cin >> ans;
}while (ans == 'Y' || ans == 'y');
//Calculations
//Clear the screen
//Display
//Hold execution on screen
system("pause");
//Indicate to OS successful termination of program
return 0;
//End main
}
Try changing this, if (guess[count] = random) to this if (guess[count] == random).
And this for (count = 0; count <= (SIZE - 1); count ++) is a little redundant. Since you only want count to go from 0 to 9, for the 10 digits, it's better written as for (count = 0; count < SIZE; count ++)
If you want to learn from this answer, be sure to apply the first fix, compile and see what happens, and then continue to the second. unless of course you're looking for a quick fix, nothing's wrong with that.
I noticed that it's not really a random number, but the number the user was looking for. and no matter what the user does, he gets the congratulations screen. so you see that there's a problem there. the program can't detect correctly if he won or lost, so go to the `if` that checks that and you can see there the problem:
Fix:
Line 78:
if (guess[count] == random)
Line 92 (also):
else if (count == (SIZE - 1))
We still get a random number, I noticed that the program does NOT ask for the last number, so this is junk that was already in the memory (when you create pointer to an unset variable, you get what was already there, and it can be any junk, and that is the number we get now). to fix this, increase the loop that gets the numbers so the last number in the `guess` array would be included.