My program isn't pausing for user input

Hey,
I'm trying to write a craps game using while loops. The problem is once in the loop the game doesn't pause for further user input. Also, to give the user the option to play again I wrote a void function, but like the loop, the program skips by any user inputs.
Any help on this pain in the butt would be most appreciated.

Thanks




#include <iostream> // for console input/output
#include <iomanip> // for setprecision
#include <ctime> // for the time function
#include <cstdlib> //for rand and srand
#include <string> // to store username

using namespace std;

void again(int);

int main()
{ //Beginning Braket
string username; // this variable will hold the users name
int useraccount; // this is hold the variable for the users account



cout<<"Let's play a game!"<<endl;
cout<<"This program simulates a game of craps."<<endl;

cout<<"Please enter your name: ";
cin>> username;

cout<<"How much money are you playing with today"<< endl; //to set up the users account

cin>>useraccount;

if(useraccount<1) //error control
{
cout<<"To pay you must enter a positive amount.";
cout<<"How much money are you playing with today?";
cin>>useraccount;
}

again(useraccount);

cout<<"Bye "<< username<<endl;

system("pause");
return 0;
} //Ending braket

void again(int useraccount)
{ //Beginning void bracket

int r; // this variable hold the dice roll
int wager; // variable to hold the users wager
char bet; // variable to hold whether the user bets on pass on no pass
srand((unsigned) time(0)); //to connect the time to the random number generator

cout<<"Bet on (P)ass or (N)o Pass "; // whether the user wished to pass or not to pass
cin>> bet;

while(!((bet == 'p') || (bet =='n'))) //User error control
{
cout<<"That is not an option. Please choose p or n. "<<endl;
cin>>bet;
}

cout<<"How much would you like to wager:"; //to declare the wager
cin>> wager;

if(wager>useraccount) //error control
{
cout<<"You do not have that much in your account."<<endl;
cout<<"How much would you like to wager:";
cin>> wager;
}


if (bet =='p')
{ //beginning of pass if statement
cout<<"Press r to roll";
cin>>r;

r= (rand()%12) +1;
cout<<"The roll is "<<r<<endl; // random variable and range

while((r == 1)||(r ==4) || (r ==5) || (r ==6) ||(r ==8) || (r ==9) || (r ==10))
{
cout<<"Enter r to roll:"<<endl;
cin>>r;
r= (rand()%12) +1;
cout<<"The roll is "<<r<<endl; // random variable and range
}

if((r == 7) || (r == 11)) //nested if statement for win
{
cout<<"Natural win!"<<endl;
cout<<"Your balance is"<< useraccount+wager<<endl;

}

if((r== 2) || (r==3) || (r==12)) //nested if statement for craps
{
cout<<"Craps! You lose!"<<endl;
cout<<"Your balance is"<< useraccount-wager<<endl;
}

} //End of pass if statement

if (bet =='n')
{ // Beginning of don't pass if statement
cout<<"Enter r to roll:"<<endl;
cin>>r;
r= (rand() % 12 ) +1; // random variable and range

cout<<"The roll is "<<r<<endl;

while((r == 1)||(r ==4) || (r ==5) || (r ==6) ||(r ==8) || (r ==9) || (r ==10))
{ //beginning while bracket
cout<<"Enter r to roll:"<<endl;
cin>>r;
r = rand() % 12+1;
cout<<"The roll is "<<r<<endl;

}
if((r == 7) || (r == 11)) //nested if statement inside bet
{
cout<<"Natural win! You lose!"<<endl;
cout<<"Your balance is"<< useraccount-wager<<endl;
}

if((r== 2) || (r==3) || (r==12))
{
cout<<"Craps! You Win!"<<endl;
cout<<"Your balance is"<< useraccount+wager<<endl;
}

} //end of no pass bracket

int p_again;
cout<<"Would you like to play again?"<<endl;
cout<<"Type y for yes and anykey to exit."<<endl;
cin>>p_again;

if(p_again =='y')
{
again(useraccount);
}
else
{
return;
}

} //Ending void bracket
r is an int, but you put a character into it. This is bad and causes something to remain in the input buffer, and then that something gets read in at the next cin call. I won't go into the details, because to be honest I'd probably get something wrong and mislead you as to exactly what happens. You can look up how the input buffer works yourself and get it firsthand.

If you want people to press "r" to roll, don't store the input character in an int. There's really no need to store their input at all in this case.

I note that you don't actually have a loop to play the game repeatedly; you are instead calling the again function each time. This is very, very bad. This is not like BASIC where you just move the execution point around in the code. Each time you call the function again, the existing function data doesn't get overwritten or deleted, but a whole new iteration of that function is started and all the data stored from that, and then again, and again, deeper and deeper each time you call the function until eventually you literally run out of memory on the stack and the program crashes.

Take a minute to read up on loops; http://www.cplusplus.com/doc/tutorial/control/
yes, thank you for the link. I put in a 'do while' function and it's working. Though I'm still having trouble with the r variable. I made it a char, but now the computer wont read the value.
Shortly afterwards, you use r to store numbers. Making it a char seems inappropriate if you want to use it for storing numbers.

cout<<"Enter r to roll:"<<endl;
cin>>r;


Just don't store this input in r. Don't store it at all, you aren't using it for anything.

1
2
cout<<"Enter r to roll:"<<endl;
cin;
Last edited on
I must be really confused. If I don't store the random number in a variable how will the computer test it?
1
2
cout<<"Enter r to roll:"<<endl;
cin>>r;

In this code above, you store what the user types in; you are storing a char input in a variable meant for an int. Don't store that. You've no need for it and trying to store an input char in a variable meant for an int causes problems for you. Don't store what the user types in here.


r= (rand()%12) +1;
In this code, you are storing the random number. That's good. You need that. Do store that.
Last edited on
Ok, I think I've fixed it. R is an int and I've made the users' choice a char. Thank you so much for your help.

int r; //to store random number
char choice; // users choice to roll
if (bet =='p')
{ //beginning of pass if statement
cout<<"Press r to roll";
cin>>choice;
choice ='r';
r= (rand()%12) +1;
cout<<"The roll is "<<r <<endl; // random variable and range

while((r == 1)||(r ==4) || (r ==5) || (r ==6) ||(r ==8) || (r ==9) || (r ==10))
{
do
{
cout<<"Enter r to roll:"<<endl;
cin>>choice;

r= (rand()%12) +1;
cout<<"The roll is "<<r<<endl; // random variable and range
} while((r == 1)||(r ==4) || (r ==5) || (r ==6) ||(r ==8) || (r ==9) || (r ==10));
Topic archived. No new replies allowed.