0

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;
}

Edited by Narue: fixed code tags

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by frogboy77
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.

Edited by frogboy77: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.