Guess game summary



I created a guessing game where a user tries to guess a number between 1-100. Once the user wins they have the option to play again or quit. I am having trouble with the game summary which is supposed to tell the average number of guesses across all the rounds, the most guesses in any given round, and least guesses in any given round. My variable "total" keeps track of all the guesses in one particular round. "maxGuess" is the most guesses in a given round, and "minGuess" is the least number of guesses in a given round.


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
#include <iostream>
#include <string>
#include <ctime>
using namespace std;

void guess(int & total,int &max, int & min);
void msg();
void gameSummary(int round, int maxGuess, int minGuess, int total);

int main(){

    char resp[10];
    int total = 0; // total number of guesses in a round
    int round = 0;  // keep track the number of rounds played
    int max;  // the maximum number of a guess
    int min;  // the minimum number of a guess
    int maxGuess = 0; //max number of guesses in all the rounds
    int minGuess = 0; // min number of guesses in all the rounds
    msg();   // display the rule of guessing game

    int total = 0;
    while(true) {
        round++;
        min = 0;
        max = 100;
        guess(total, max,  min);

        printf("\n\n\n\tDo you want to play another round? ");
        cin >> resp;
        cout << endl;// skips a line
        if ( resp[0] == 'y' || resp[0] == 'Y' ) continue;
        else break;
    }

    gameSummary(round, maxGuess, minGuess, total);
    printf("\n\tThank you for playing Guessing Game.\n\n");
    system("pause");
    return 0;
}

void msg(){
    cout << "\tWelcome to the guessing game\n"
         << "\tI will choose a number betweeen 1 and 100 ...and you try     to guess it\n\n"
         << "\tWith each guess, I will tell you whether you are high or low\n"
         << "\tThe obejective is to find the number using as few guesses as possible\n\n\n\n\n"
         << "\tI am thinking of a number between 1 and 100\n\n";
}

void guess(int &total, int &max, int &min) {
    int up = 100;
    int low = 0;

    srand((unsigned)time(0));
    int number = rand() % (up - low + 1) + low;


    int guess;
     total= 0;


    while (true) {
        // Getting new guess
        cout << "\tPlease enter your guess :";
        cin >> guess;
        total++;


        // Handling limit
        if (total == 100) {
            cout << "guess limit reached" << endl;
            break;
        }

        // Handling new guess
        cout << endl;
        if (guess == number) {
            cout << "congratulations!! " << guess << " is the number!!\n"
                << "Wow!! it took you only " << total << " attempts to get the right number\n";
            break;
        }
        else if (guess > number && guess < max) {
            max = guess;
            cout << "\tSmaller than " << guess << endl;
        }
        else if (guess < number && guess > min) {
            min = guess;
            cout << "\tLarger than " << guess << endl;
        }
        else{
            cout << "Please enter an integer between " << min << " and " << max << endl;
        }
    }
}



void gameSummary(int round, int maxGuess, int minGuess, int total){



    cout << "\tGame Summary:\n\n";
    cout << "\tTotal number of rounds :" << round << endl << endl;
    cout << "\tAverage number of guesses per round : " << (total/round)   << endl << endl;
    cout << "\tThe most number of guesses in one round : " << maxGuess << endl;
    cout << "\tThe least number of guesses in one round : " << minGuess << endl;

}
Last edited on
Line 28,36: Why are you using printf in a C++ program?

Line 53: Do not call srand() within a loop (line 22). srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/
Topic archived. No new replies allowed.