### 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?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697`` ``````#include #include #include 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 "<> 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 "<
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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384`` ``````#include #include #include #include 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<<" "<> 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 "<
Topic archived. No new replies allowed.