Need help with 1 tiny error on my prog

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!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**************************** Compiler Directives *****************************/

#include <cstdlib>
#include <iostream>
#include <ctime>
using namespace 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
    const int SIZE = 10;        //Sets the size of the array/chances
    const int MIN = 1;       //Change if lower range is needed
    const int 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;
            }
            else if (guess[count] < random)
            {
                cout << guess[count] << " is low. Go higher." << endl;
                count++;
            }

            else if (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;
                }
        }

        else if (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
}
@thechadmartinez

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.

Fix:
Line 76:

}while(count != (SIZE) && guess[count] != random);

Line 92:
else if (count == (SIZE))

And the program is perfect ;)
Last edited on
Thank you so much hadar! Program is perfecto!! THANK YOU! I CANNOT EXPLAIN HOW 4 HOURS OF MY ANALYZING TOOK YOU LESS THAN 15 MINUTES!

Thank you all!
Topic archived. No new replies allowed.