cup game program

hi friends i have to create a program which generates 6 random numbers and gives 3 tries to the user after which it will display that you have lose the game in c++ the following code i have constructed but i am stuck with the resetting the number of tries once the game is over or the game is been won the following is the code that i have:

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
int guses=1;
int number;
int tries=0;
char answer;
int main(){
cout<<"\t\t\tWelcome to the cup and a ball game\n\n";
cout<<"You have to guses a number between 1 to 6 and if you are correct u win!!!!!\n\n";

while (answer!='Q'&& answer!= 'q'){
tries++;
if (tries==4){
cout<<" you lose!!!! please restart the game...\n";
return tries;
}
cout<<"please enter 'Y' to begin the game and 'Q' to quit\n";
cin>>answer;
if (answer=='Y' || answer=='y'){
cout<<"please enter a number between 1-6\n";
cin>>guses;
srand (time(0));
number=rand()%6+1;
if(number==guses){
cout<<"congrats you have won the game\n";
cout<<"random number is = "<<number<<endl;
return tries;
}
else{
cout<<" please try again\n";
cout<<"random number is = "<<number<<endl<<endl;
}
}

}

return 0;
}
closed account (GTbMSL3A)
I like playing cup games with people's cups.

the program's quite hard to read. a few spaces and indents might make it easier on the eyes. i'll try to see if anything's wrong with a quick peek.

Edit:
but i am stuck with the resetting the number of tries once the game is over or the game is been won


you can try to implement your game as a function.

then you can ask the user if he wants to continue. if the user answers yes, then the function is executed again. this can be done with a while-loop that checks the value of a string.
Last edited on
Topic archived. No new replies allowed.