954,483 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Number Guessing game

hello, all!
I am an absolute rookie in the world of C++ language, and not a good one either... I have an assignment to create a random number guessing game, and I am struggling with it. I have most of it done, but we have to generate the number of tries when the user wins, the number of games played, won,and the percentage of games won. unfortunately, when I play more than once, the tries add up, and I do not know how to count them out of the loop. The percentage does not seem to be working either. here is what I have so far:

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

using namespace std;

const int EXIT_VALUE = -1,  MIN_NUMBER = 1,  MAX_NUMBER = 100,  MAX_GAMES = 256;

int main()
{

int guess;
char answer='y'; 
int lostCount = 0;
int totalGames = 0;
int tries = 0;
int winGames = 0;
double averageWin = 0.0;
 
{
 

do {

srand(time(NULL));
int randomNumber = MIN_NUMBER + rand() % (MAX_NUMBER - MIN_NUMBER + 1);




cout<<"Im thinking of a number between 1-100. Take a guess: ";
cin>>guess;
tries++;
	while(guess!=randomNumber)
	

	{
	if(guess == EXIT_VALUE)
           
                 {  cout << "Too bad... The number was:" << " " << randomNumber << endl;
                 lostCount++;
                 cout << "would you like to play again?" << endl;
                 cin >> answer;
                 if (answer == 'Y' || answer == 'y')
                 cout << "please enter a number between 0 and 100:" << endl;
                 cin >> guess;
                  
                 
                  }
	
		if(guess>randomNumber)
		{
			cout<<"Too high,Guess again: ";
			cin>>guess;
			++tries;
		}
		else if(guess<randomNumber)
		{
			cout<<"Too low,Guess again: ";
			cin>>guess;
		++tries;
		}
	}
		if(guess=randomNumber)
		{
			cout<<"Congratulations!! You found the number!";
			
			cout << "it took you" << " " << tries << " " << "tries to find the number!" << endl;
		winGames++;
}
 
		cout << "Would you like to play again? y/n ";
		cin >> answer;
		} while (answer =='y' || answer =='Y');
		totalGames = winGames + lostCount;
		averageWin = winGames / 100;
		if (answer == 'n' || answer == 'N')
	cout << "Thank you for playing! you have played" << " " << totalGames << " " << "games,"<< endl;
  cout << " won" << " " << winGames << " " << "games, and the average of your winning games is";
  cout << " " << averageWin << " " << "%" << endl; 
  }
  system ("pause");

return 0;
}
ChanelN19
Newbie Poster
1 post since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

I don't understand what you mean by generate the number of tries when the user wins
Should line 76 not be averageWin=(winGames/totalGames)*100;
The percentages will not work if you are using ints. i.e 36 games won, 72 games played
36/72=0........... 0*100=0
Use doubles.

frogboy77
Posting Pro in Training
481 posts since Aug 2010
Reputation Points: 100
Solved Threads: 39
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: