Need help getting avg number of guesses.

Its a guess game with 4 rounds.. i just need to find the average number of guesses. I DO NOT need the number of guesses for each round which i already have.

Here is my code. What am i doing wrong?

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;



int main()
{
    srand(time(0));


    int start;
    int end;

    cout<<"Enter the starting value for what your guess can be between ";
    cin>>start;
    cout<<"Enter the ending value for your guess can be between ";
    cin>>end;

    int range;
    range=(start-end);
    int secretNumber = (rand() % range) + start;

    int NumberOfGuesses = 0;
    int NumberOfGames = 0;
    int AvgGuesses;
    int guess;



do {



    cout<<"Round "<<NumberOfGames+1<<" "<<endl;
    ++NumberOfGames;


    do
    {
        cout << "Enter a guess: ";
        cin >> guess;
        ++NumberOfGuesses;
        




            if (guess > secretNumber)
            {
                cout << "Too high!" << endl << endl;

            }

            else if (guess < secretNumber)
            {
                cout << "Too low!" << endl << endl;

            }

             } while (guess != secretNumber);

              if (guess == secretNumber)



    {
        cout << "You win! You got it in " << NumberOfGuesses << " guesses" << endl;
        secretNumber = (rand() % range) + start;
        NumberOfGuesses=0;




    }



      }while (NumberOfGames<4);

      AvgGuesses=NumberOfGuesses/NumberOfGames;
      cout<<"Average number of guesses "<<AvgGuesses;

}










Last edited on
Well I'm embarrassed to say this but this took me a lot longer than I thought it would (The average part) but I managed to do it so here's the your program with an average guesses calculation. Please feel free to ask any questions you want. You were just putting the average part in the wrong spot, plus you have to treat it like a sum variable.

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

int main()
{
    randomize();


    int start;
    int end;

    cout<<"Enter the starting value for what your guess can be between ";
    cin>>start;
    cout<<"Enter the ending value for your guess can be between ";
    cin>>end;

    int range;
    range=(end-start);
    int secretNumber = (rand() % range) + start;

    int TotalNumberOfGuesses = 0;
    int NumberOfGuesses = 0;
    int NumberOfGames = 0;
    int AvgGuesses = 0;
    int guess = 0;



do {

    clrscr();

    cout<<"Round "<<(NumberOfGames)+1<<" "<<endl;
    ++NumberOfGames;
    NumberOfGuesses=0;

    do
    {   ++NumberOfGuesses;
        cout << "Enter a guess: ";
        cin >> guess;




            if (guess > secretNumber)
            {
                cout << "Too high!" << endl << endl;

            }

            else if (guess < secretNumber)
            {
                cout << "Too low!" << endl << endl;

            }

             } while (guess != secretNumber);

              if (guess == secretNumber&&NumberOfGames<4)



    {   TotalNumberOfGuesses=TotalNumberOfGuesses+NumberOfGuesses;
        cout <<"You win this round! You got it in " << NumberOfGuesses << " guesses" << endl;
        secretNumber = (rand() % range) + start;
        getch();


    }



      }while (NumberOfGames<4);
      clrscr();TotalNumberOfGuesses=TotalNumberOfGuesses+NumberOfGuesses;
      cout << "You win! You got it in " << TotalNumberOfGuesses << " guesses" << endl;
      AvgGuesses=TotalNumberOfGuesses/NumberOfGames;
      cout<<"Your average number of guesses is "<<AvgGuesses;
      getch();
      return 0;

}
Topic archived. No new replies allowed.