You are here:

C++/C++ Number Guess Program !

Advertisement


Question
i am a newbie in this so please bear with me.  i have to write a program on a guessing the number game.  i have the program written but i need help.  i have to write a program that plays the guessing game higher or lower.  you get 10 chances to guess the number correctly( i think i have that now) but what i need help with is the next part.  after the user runs out of guesses or guesses the right number the program should ask if you want to play again and react accordingly.  at the end of the program it should display some summary information, including the following:
number of gamesplayed, number of correct guesses, average number of guesses it took to get the right answer.  on my program now when i go to build a a solution and debug it gives me an error message RUN TIME CHECK FAILURE #3 THE VARIABLE 'noofgamesplayed' IS BEING USED WITHOUT BEING DEFINED.  any help with project would be greatly appreciated.  here is what i have done so far.  again, thank you for any help in this matter.

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main ()
{
int num;
int guess;
bool done;
int noOfGuesses=0;
int ncount=0;
int sum=0;
int noofgamesplayed=0;
int avgNoOfGuesses=0

;sum+=noOfGuesses;
avgNoOfGuesses=sum/noofgamesplayed;

num = (rand() + time(0)) % 1000;
done = false;
while ((noOfGuesses < 10) && (!done))

{
cout << "Enter an integer greater"
<< " than or equal to 0 and "
<< "less than 1000: ";
cin >> guess;
cout << endl;
noOfGuesses++;
if (guess == num)
{
cout << "you guessed the correct "
<< "number." << endl;
done = true;
}
else
if (guess < num)
cout << "Your guess is lower "
<< "than the number. \n"
<< "Guess again!" << endl;
else
cout << "Your guess is higher "
<< "than the number.\n"
<< "guess again!" << endl;
cout <<"Total gueses equal " << noOfGuesses << endl;



}
return 0;
}  

Answer
Dear Mr Mike !

 Problem with your program is the set of statements given below !
>>sum+=noOfGuesses;
>>avgNoOfGuesses=sum/noofgamesplayed;

as per your program the initial values of  sum , noOfGuesses , noofgamesplayed is 0 while evaluating the expression
>>avgNoOfGuesses=sum/noofgamesplayed; ( ie 0/0 ) as you know very well this is a mathematical error !

Here Im presenting you the modified version of your program , hope will meet your requirement ! , if you find any bugs with this program , feel free to report me

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int main ()
{
  int num;
  int guess;
  bool done;
  int noOfGuesses=0;
  int ncount=0;
  int sum=0;
  int noofgamesplayed=0;
  int avgNoOfGuesses=0;
  int opt;
  
  
  
  do
  {
     num = (rand() + time(0)) % 1000;
     done = false;
     ++noofgamesplayed;
     noOfGuesses = 0;

     while ((noOfGuesses < 10) && (!done))
     {
        cout << "Enter an integer greater"
           << " than or equal to 0 and "
           << "less than 1000: ";
        cin >> guess;
        cout << endl;
        noOfGuesses++;
        if (guess == num)
        {   
           cout << "you guessed the correct "
              << "number." << endl;
           done = true;
        }
        else
           if (guess < num)
              cout << "Your guess is lower "
              << "than the number. \n"
              << "Guess again!" << endl;
           else
              cout << "Your guess is higher "
                 << "than the number.\n"
                 << "guess again!" << endl;
              cout <<"Total gueses equal " << noOfGuesses << endl;
     }

     sum+= noOfGuesses ;

  cout<<"\nLike to try the game again <1/2>?";
  cin>>opt;
  } while( opt != 2);

  cout<<"\n Number of Games played "<<noofgamesplayed;
  cout<<"\n Total number of guesses :"<< sum;
  cout<<"\n Guessing Average = :"<<sum/noofgamesplayed;
  return 0;
}

Note : Tune up this program as per ur needs !

Thanks and Regards !
Prince M. Premnath  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Prince M. Premnath

Expertise

Years of research in C/C++ Will bring you all facts regarding , OOPS , SVGA , MOUSE , BIOS , IVT , Feel free to query me :-)

Experience

More than 5 years

Education/Credentials
MCA

©2012 About.com, a part of The New York Times Company. All rights reserved.