Hey guys, I am having a problem with this program. It's actually part of an example from Michael Dawson's Beginning c++ Game Programming
This program pretty much randomized a number between 1 and 100 and has the user input his/her guess. The user can deduce what the number is each time he or she guesses incorrectly because the program will output a "too high" or "too low" message if the number is either greater then or less than respectively. If the user guesses correctly then the loop is broken and the user is given the number and the amount of tries it took to guess the number correctly. (I went ahead and let the program output the number..not because I am a cheater, it's just nice to know the number b/c it makes it easier to test the program for potential mistakes)
I went ahead an followed his instruction, but decided to spice up the code a bit by having the program recognize repeated numbers and output a message to inform the user that he/she repeated a guessed number.
This all works fine and dandy...but for some reason, after 7 wrong guesses, the console screen crashes.
This only occurred after I put the statements between //run check and //end check
Does it have something to do with the array not being initialized?...
int main()
{
//initialize and seed random function
srand(time(NULL));
int randomize = (rand() % 100) + 1; //assigns random value to randomize variable
cout << "****Guess a number from 1 to 100****" << endl;
cout << randomize;
int x = 0;
int tries = 0;
int guess[x];
do
{
++x; //incriment to make room for next array slot
cout << "\nTake a guess: ";
cin >> guess[x];
int judged = 0;
currentGuess = guess[x];
// run check
int y = 0;
for(y ; y < x; ++y) //just use y to increment and check
{
if(currentGuess == guess[y])
{
//display repeated number
cout << "Already guessed " << guess[y] << endl;
}
}//end for
//end check
++tries;
if(guess[x] > randomize)
{
cout << "Too high";
}
if(guess[x] < randomize)
{
cout << "Too low";
}
}while(guess[x] != randomize);
cout << "Well done! The number was indeed " << randomize << endl;
cout << "Number of tries: " << tries;
cin.get();
}
I am using code blocks as my IDE.
Thanks in advance.
(Note that this is not a HW assignment, I simply picked up c++ three or four weeks ago for fun, so far it has been!)