This program is about dice 21. The problem i have is when i go to the second round it adds up the total turn value i got from the first round, and I dont want that to happen I want it to start again at 0. I still need to more to the program but I will do that myself I just need for some help to fic the problem.
If you pass 21 you lose and if your total score is 21 you win
#include<iostream>
#include<cstdlib>
#include<time.h>
usingnamespace std;
int getRandomNum( int lowRange, int highrange);
int main()
{
char choice=' ';
char keep=' ';
srand( static_cast<int>( time(NULL) ) );
///////////////////////////// Start of the code
cout<<"**************** Welcome to the Dice 21 game! ****************"<<endl;
cout<<endl;
cout<<"You must win the majority of rounds to win the game. In each";
cout<<"round you will roll one 10-sided die as many times as you like.";
cout<<"Each time you roll, the number on the die will be added to your";
cout<<"total score. Your goal is to get a score as close as possible to";
cout<<"21 without going over 21.";
cout<<endl;
cout<<endl;
cout<<"At the end of a round, you will be able to look ahead to see";
cout<<" what the next roll would have been. You win the round if the";
cout<<" next roll would have caused you to go bust. Otherwise you lose";
cout<<" the round"<<endl;
cout<<endl;
cout<<"********** Round 1 *********"<<endl;
int rollValue= 0;
int total_turn= 0;
int value1= 0;
int value2= 0;
do
{
if(rollValue==1)
{
cout<<"You have rolled a 1. It's value could be 1 or 11"<<endl;
cout<<"Enter 'y' if you would like the value to be 11"<<endl;
cout<<"Enter any other character if you would like to keep the value 1"<<endl;
cin>> keep;
if(keep=='y')
{
value1= 11;
cout<<"You rolled:"<<value1<<endl;
total_turn= total_turn + value1;
cout<<"Your roll total is:"<<total_turn<<endl;
cout<<endl;
}
else
{
value2= 1;
cout<<"You rolled:"<<value2<<endl;
total_turn= total_turn+ value2;
cout<<"Your roll total is :"<< total_turn<<endl;
cout<<endl;
}
}
cout<<"Rolling..."<<endl;
rollValue = rand() % 10 + 1;
cout<<"You rolled a :"<<rollValue<<endl;
total_turn= total_turn + rollValue;
cout<<"Your roll tatal is :"<<total_turn<<endl;
cout<<endl;
cout<<"Do you want to roll again: (y/n)";
cin>> choice;
if(total_turn==21)
{
cout<<"WINNERR YOU ROLLED 21!!!"<<endl;
break;
}
elseif(total_turn>21)
{
cout<<"Sorry you lose you rolled over 21"<<endl;
break;
}
if(choice=='y')
{
if(rollValue==1)
{
cout<<"You have rolled a 1. It's value could be 1 or 11"<<endl;
cout<<"Enter 'y' if you would like the value to be 11"<<endl;
cout<<"Enter any other character if you would like to keep the value 1"<<endl;
cin>> keep;
if(keep=='y')
{
value1= 11;
cout<<"You rolled:"<<value1<<endl;
total_turn= total_turn + value1;
cout<<"Your roll total is:"<<total_turn<<endl;
cout<<endl;
}
else
{
value2= 1;
cout<<"You rolled:"<<value2<<endl;
total_turn= total_turn+ value2;
cout<<"Your roll total is"<< total_turn<<endl;
cout<<endl;
}
}
cout<<"Rolling..."<<endl;
rollValue = rand() % 10 + 1;
cout<<"You rolled a :"<<rollValue<<endl;
total_turn= total_turn + rollValue;
cout<<"Your roll tatal is :"<<total_turn<<endl;
if(total_turn==21)
{
cout<<"WINNERR YOU ROLLED 21!!!"<<endl;
break;
}
elseif(total_turn>21)
{
cout<<"Sorry you lose you rolled over 21"<<endl;
break;
}
cout<<endl;
cout<<"Do you want to roll again: (y/n)";
cin>> choice;
}
elseif(choice=='n')
{
cout<<"Your roll total is:"<<total_turn<<endl;
rollValue = rand() % 10 + 1;
cout<<"Look ahead to the next roll..."<<endl;
cout<<"Your next roll would have been:"<<rollValue<<endl;
total_turn=total_turn+rollValue;
if(total_turn>21)
{
cout<<"Good choice you would have one BUST, if you have rolled again"<<endl;
break;
}
elseif(total_turn<21)
{
cout<<"BUMMER- you could have rolled again"<<endl;
break;
}
}
else
{
cout<<"invalid input"<<endl;
break;
}
}while(total_turn<= 21);
cout<<"********** Round 2 *********"<<endl;
do
{
if(rollValue==1)
{
cout<<"You have rolled a 1. It's value could be 1 or 11"<<endl;
cout<<"Enter 'y' if you would like the value to be 11"<<endl;
cout<<"Enter any other character if you would like to keep the value 1"<<endl;
cin>> keep;
if(keep=='y')
{
value1= 11;
cout<<"You rolled:"<<value1<<endl;
total_turn= total_turn + value1;
cout<<"Your roll total is:"<<total_turn<<endl;
cout<<endl;
}
else
{
value2= 1;
cout<<"You rolled:"<<value2<<endl;
total_turn= total_turn+ value2;
cout<<"Your roll total is :"<< total_turn<<endl;
cout<<endl;
}
}
cout<<"Rolling..."<<endl;
rollValue = rand() % 10 + 1;
cout<<"You rolled a :"<<rollValue<<endl;
total_turn= total_turn + rollValue;
cout<<"Your roll tatal is :"<<total_turn<<endl;
cout<<endl;
cout<<"Do you want to roll again: (y/n)";
cin>> choice;
if(total_turn==21)
{
cout<<"WINNERR YOU ROLLED 21!!!"<<endl;
}
elseif(total_turn>21)
{
cout<<"Sorry you lose you rolled over 21"<<endl;
}
if(choice=='y')
{
if(rollValue==1)
{
cout<<"You have rolled a 1. It's value could be 1 or 11"<<endl;
cout<<"Enter 'y' if you would like the value to be 11"<<endl;
cout<<"Enter any other character if you would like to keep the value 1"<<endl;
cin>> keep;
if(keep=='y')
{
value1= 11;
cout<<"You rolled:"<<value1<<endl;
total_turn= total_turn + value1;
cout<<"Your roll total is:"<<total_turn<<endl;
cout<<endl;
}
else
{
value2= 1;
cout<<"You rolled:"<<value2<<endl;
total_turn= total_turn+ value2;
cout<<"Your roll total is"<< total_turn<<endl;
cout<<endl;
}
}
cout<<"Rolling..."<<endl;
rollValue = rand() % 10 + 1;
cout<<"You rolled a :"<<rollValue<<endl;
total_turn= total_turn + rollValue;
cout<<"Your roll tatal is :"<<total_turn<<endl;
if(total_turn==21)
{
cout<<"WINNERR YOU ROLLED 21!!!"<<endl;
}
elseif(total_turn>21)
{
cout<<"Sorry you lose you rolled over 21"<<endl;
}
cout<<endl;
cout<<"Do you want to roll again: (y/n)";
cin>> choice;
}
elseif(choice=='n')
{
cout<<"Your roll total is:"<<total_turn<<endl;
rollValue = rand() % 10 + 1;
cout<<"Look ahead to the next roll..."<<endl;
cout<<"Your next roll would have been:"<<rollValue<<endl;
total_turn=total_turn+rollValue;
if(total_turn>21)
{
cout<<"Good choice you would have one BUST, if you have rolled again"<<endl;
}
elseif(total_turn<21)
{
cout<<"BUMMER- you could have rolled again"<<endl;
}
}
else
{
cout<<"invalid input"<<endl;
break;
}
}while(total_turn<= 21);
system("pause");
return 0;
}
I don't know about making each loop shorter but since round 1 code is virtually the same as round 2 code you could probably make a loop to repeat round 1 code instead.
Also in your original code you should make rollValue = 0 for the start of round 2 also.