Here is my codes,, somehow program does not work and I dont know how to add percentage part into the code..and I think there are some errors in code//
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
|
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;
int r_number(int interval) {
srand(time());
int randomNumber = MIN_NUMBER + rand() % interval;
return randomNumber;
}
again()
{
const int MAX_GAMES = 256;
int decision;
cout<<"Do you want to play again? (y/n): ";
cin>>decision;
MAX_GAMES--;
if(MAX_GAMES==0){
return EXIT_SUCCESS;
}
return decision;
}
int main(int,char**){
int gnumber;
int dnumber;
const int MIN_NUMBER = 1;
const int MAX_NUMBER = 100;
const int EXIT_VALUE = -1;
const int MAX_GAMES = 256;
int counter = 5;
char decision2;
system("color 2b");
cout <<"*** You are playing the CSC100 Guessing Game ***"<<endl<<endl;
dnumber = r_number(100);
ask:
do{
cout<<"Enter a number between 1 and 100 (-1 to give up): ";
cin>>gnumber;
if(counter!=0){
if((gnumber>100) || (gnumber<1))
{ cout<<gnumber<<" is too big"<<endl;
goto ask;
}
if(gnumber==-1){
cout<<"*** QUITTER ***"<<endl;
cout<<"The number was "<<dnumber<<endl;
decision2= again();
}
if(gnumber==dnumber)
{
cout<<"**GOT IT** it took you "<<5-counter<<" guesses"endl;
counter=5;
decision2 = again();
dnumber=r_number(100);
}
else if(gnumber < dnumber)
{
cout<<"nope.."<<endl;
counter--;
goto ask;
}
else if(gnumber>dnumber)
{
cout<<"nope..."<<endl;
counter--;
goto ask;
}
}
else if(counter<=0)
{ if(gnumber < dnumber){
cout<<"nope....higher.."<<endl;
}
else if(gnumber > dnumber){
cout<<"nope....lower.."<<endl;
}
else if(gnumber==dnumber) {
cout<<"**GOT IT** it took you "<<5-counter<<" guesses"<<endl;
counter=5;
dnumber=r_number(100);
decision2 = again(); }
}
}while(decision2 == 'Y' || 'y');
cout<<"Thanks for playing the CSC100 guessing game."<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
|
And here is my assingment, I tried to follow the directions but...I dont know/
Write a program that allows the user to play guessing games.
A guessing game is where the computer generates a random number and the user tries to guess it. The program loops until the user guesses the random number, or enters a sentinel value to give up. After five wrong guesses, the user is given help (higher or lower messages).
After a game has been completed, the program asks the user if they want to play again. The user is allowed to play at most 256 games.
Prior to terminating program, print the following.
number of games played
number of games won
winning percentage
Required Manifest Constants
Your program must define the following manifest constants prior to the definition of the main() function (i.e. make these global):
const int MIN_NUMBER = 1;
const int MAX_NUMBER = 100;
const int EXIT_VALUE = -1;
const int MAX_GAMES = 256;
Generating a Random Number
Include the time.h header file. To generate a random number that ranges between MIN_NUMBER and MAX_NUMBER (inclusive), use code similar to the following.
srand(time(NULL));
randomNumber = MIN_NUMBER + rand() % (MAX_NUMBER - MIN_NUMBER + 1);
The time() function returns the current date and time in seconds since 01-Jan-1970 00:00:00 GMT. Passing this return value to srand() "seeds" the random number generator so that a different set of random numbers are generated every time the program is executed. The rand() function returns a random number between 0 and RAND_MAX (a manifest constant defined in stdlib.h).
Help: time() | srand() | rand()
Example Game
Assume the computer generated random number is 34 for game #1, 73 for game #2, and 99 for game #3. For example purposes, user inputs are enclosed in brackets <>.
*** You are playing the CSC100 Guessing Game ***
Enter a number between 1 and 100 (-1 to give up): <3>
nope...
Enter a number between 1 and 100 (-1 to give up): <1001>
1001 is too big...
Enter a number between 1 and 100 (-1 to give up): <0>
0 is too small...
Enter a number between 1 and 100 (-1 to give up): <33>
nope...
Enter a number between 1 and 100 (-1 to give up): <50>
nope...
Enter a number between 1 and 100 (-1 to give up): <21>
nope...
Enter a number between 1 and 100 (-1 to give up): <41>
nope...
Enter a number between 1 and 100 (-1 to give up): <27>
nope...higher
Enter a number between 1 and 100 (-1 to give up): <57>
nope...lower
Enter a number between 1 and 100 (-1 to give up): <34>
*** GOT IT *** it took you 8 guesses
Do you want to play again? (y/n): <y>
Enter a number between 1 and 100 (-1 to give up): <99>
nope...
Enter a number between 1 and 100 (-1 to give up): <-1>
*** QUITTER ***
Do you want to play again? (y/n): <y>
Enter a number between 1 and 100 (-1 to give up): <21>
nope...
Enter a number between 1 and 100 (-1 to give up): <99>
*** GOT IT *** it took you 2 guesses
Do you want to play again? (y/n): <n>
Thanks for playing the CSC100 guessing game.
You played 3 games and won 2 of them.
Your winning percentage is 66.7%.