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