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!)

You should definitely look into using a std::vector<int> instead of an array of ints. You can push_back() each element so the vector will grow to accommodate the new guesses. You could also use an std::set instead of an std::vector to "automatically" tell if a number had already been guessed. If the size of the set doesn't change when you insert(), then the number was already in the set!

Let us know if you have problems trying any of this.

Good luck,

Dave

You seem to have the right idea, but you are defining your guess array to be an array of integers that is (0) elements long. It's not technically illegal, but it's very unsafe.

There might be a better way to do it, but I would suggest you treat it more like analyzing a distribution. Create a bool array with 101 elements (elements 0-100), then each time the user enters a legal guess, you use the guess as the array element to look at. If the element is true, the guess has been used. If the element is false, set it to true and then continue to process accordingly.

[edit]
A little slow. It looks like dave's suggestion is a better one.

I guess it depends on the logic of the program. You may want to use Fbody's suggestion of a bool array because say the number you guess is greater than the secret number - you'd want to mark ALL of the number above the guess as "used" because you've already effectively guessed that whole range.

I am going to go ahead and close the thread b/c both of these suggestions seem legit, problem is I haven't gone over vectors yet (the first c++ book I used was far too elementary I guess), but currently I am self studying Stephan Prata's c++ Primer Plus and in the next chapter he discusses vectors, so I guess I can do try David's suggestion then.

As for Fbody's reccomendation, I'll give it a shot some time tonight (my eyes BURN from staring at my computer too long). I will give each of you a heads up on what I come up with in the coming days...it will be fun to approach this a couple of different ways.

Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.