Random Number guessing game enhancement

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
// Reaper

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int main()
{
    srand(unsigned(time(NULL)));
    int num1;
    int answer;
    int tries;

    cout << "This is a number guessing game.\n\n";

    num1 = rand() % 100 + 1;

    do
    {
        cout << "Enter your answer: ";
        cin >> answer;

        cout << "\n\n";

        if (answer > num1)
        {
            cout << "Your guess is too high. Try again!" << endl;
            tries++;
        }
        else if (answer < num1)
        {
            cout << "Your guess is too low. Try again!" << endl;
            tries++;
        }
        else
        {
            cout << "You guessed my number. Congratulations! My number was " << num1 << endl;
            cout << "It took you " << tries << " tries to guess my number." << endl;
        }
    } while (answer != num1);

    return 0;
}


My random number guessing game works fine, the only problem is i cant seem to get int tries to keep a count of their gusses, anyone know why.

right now i get a random number for tries when the game ends i get the number 499915 for tries.
Last edited on
closed account (2wpSLyTq)
initialize int tries = 0;
oh wow, thank you jrthekid5, didnt realize i had to make that 0 first. it works now.
closed account (2wpSLyTq)
no prob
Topic archived. No new replies allowed.