C++ Please help, very small answer..
I wrote the codes but I couldnt add that part
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.
Here is my own codes,,
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Programmer: XXXXXXXXxxxx *
* Assignment: GuessingGame0 *
* Description: A program that allows the user to play guessing games *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include <cstdlib>
#include <iostream>
#include <time.h>
using namespace std;
int r_number(int interval ) {
const int MIN_NUMBER = 1;
const int MAX_NUMBER = 100;
srand(time(NULL));
int randomNumber = MIN_NUMBER + rand() % interval;
return randomNumber;
}
char again()
{
int MAX_GAMES = 256;
char decision;
if(MAX_GAMES>=0) {
cout<<"Do you want to play again? (y/n): "<<endl;
cin>>decision;
MAX_GAMES--;
}
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;
double lost=0;
double game=0;
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==-1){
cout<<"*** QUITTER ***"<<endl;
cout<<"The number was "<<dnumber<<endl;
game++;
lost++;
dnumber=r_number(100);
decision2= again();
}
else if(gnumber>100)
{ cout<<gnumber<<" is too big"<<endl;
goto ask;
}
else if(gnumber<1){
cout<<gnumber<<" is too small"<<endl;
goto ask;
}
else if(gnumber==dnumber)
{
cout<<"**GOT IT** it took you "<<6-(counter)<<" guesses";
cout<<endl;
counter=5;
game++;
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;
counter--;
goto ask;
}
else if(gnumber > dnumber){
cout<<"nope....lower.."<<endl;
counter--;
goto ask;
}
else if(gnumber==dnumber) {
cout<<"**GOT IT** it took you "<<6-counter<<" guesses"<<endl;
counter=5;
game++;
dnumber=r_number(100);
decision2 = again(); }
}
}while((decision2 == 'Y') || (decision2=='y'));
cout<<"Thanks for playing the CSC100 guessing game."<<endl;
cout<<"You played "<<game<<" gamed and won "<<game-lost<<" of them"<<endl;
cout<<"Your winning percentage is "<<((game-lost)/game)*100<<"%"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
|
Last edited on
I already wrote that part
1 2 3 4 5 6 7 8 9 10 11 12 13
|
char again()
{
int MAX_GAMES = 256;
char decision;
if(MAX_GAMES>=0) {
cout<<"Do you want to play again? (y/n): "<<endl;
cin>>decision;
MAX_GAMES--;
}
return decision;
}
|
but it doesnt work..it evaluates every time 255. Like 256-1=255 and 256 again and 256-1=255....
How could I fix that part??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
char again()
{
static int MAX_GAMES = 256;
char decision;
if(MAX_GAMES>0) {
cout<<"Do you want to play again? (y/n): "<<endl;
cin>>decision;
MAX_GAMES--;
}
else
decision = 'n' ;
return decision;
}
|
Last edited on
I learned something new.. it worked thank you cire ;)
Topic archived. No new replies allowed.