Craps cant get the bet to work

ok im doing a dice game and ive came to a problem when i start the game i want the user to be able to quit when they want but i got it to the point where it only allows you to enter in a letter no numbers for the bet....but what im trying to do is get it so i can bet or quit if i want...heres my code


# include <iostream>
# include <cmath>
# include <iomanip>
# include <cstdlib>
using namespace std;
int main()
{
int die1;
int die2;
int dice;
int bet;
int point;
int money = 100;
char roll;
char betChar,q,Q;

do
{
cout << "You currently have $" << money << " on hand.";

cout << "\nPlace your bet and roll the dice (minimum $1 - 'q' to quit): ";
cin >> betChar;
if (betChar == q || betChar == 'Q');
exit(1);
bet = betChar;

while (bet < 1 || bet > money)
{
if (bet < 1)
cout << "\nDont be so cheap put some money on the table!";
if (bet > money)
cout << "\nYou don't have that much!";
cout << "\n\nPlace your bet and roll the dice (minimum $1): ";
cin >> bet;
}

cout << "You bet $" << bet << ".";


cin.get(roll);

die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
dice = die1 + die2;

cout << "\nYou rolled a " << die1 << " and a " << die2
<< " for a total of " << dice << "."<<endl<<endl;

if (dice == 7 )
{
cout << "Youve won";
money += bet;
}


else if (dice == 11)
{
cout << "Youve lost";
money -= bet;
}

else
{
do
{
cout << "You currently have $" << money << " on hand."<<endl; //copied the betting loop you had
cout << "\nPlace your bet and roll the dice (minimum $1 - 'q' to quit): ";
cin >> betChar;
if (betChar == q || betChar == 'Q');
exit(1);
bet = betChar;
while (bet < 1 || bet > money)
{
if (bet < 1)
cout << "\nC'mon, take a chance!";
if (bet > money)
cout << "\nYou don't have that much!";
cout << "\n\nPlace your bet (minimum $1): ";
cin >> bet;
}
cout << "You bet $" << bet << "."; //end betting loop

cin.get(roll);

die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
dice = die1 + die2;

cout << "\nYou rolled a " << die1
<< " and a " << die2
<< " for a total of " << dice << "."<<endl;

if (dice == 7)
{
cout << "\nYOU WON!!!! lets raise the Stakes"<<endl;
money += bet;
}

else if (dice == 11)
{
cout << "\nYOU LOSE its ok you can WIN it back"<<endl;
money -= bet;
}

else
{
cout << "\nCooooommmee on 7. "<<endl;

}

} while (dice != point || dice != 7);
}
} while (money > 0);

cout << "/nLooks like you ran out of money. "
<< "Thanks for playing!" << endl;




system("pause");
}
OK, the first problem is the code to check the bet or quit.

1
2
3
4
5
cout << "\nPlace your bet and roll the dice (minimum $1 - 'q' to quit): ";
cin >> betChar;
if (betChar == q || betChar == 'Q');
exit(1);
bet=betChar;


The q should have quotes round it (as you are trying to check if ti matches the character q, not the varaible q).
The ; after the if ends the statement, so exit(1); is nto conditional - it will always occur.
So you wanted
1
2
3
4
5
cout << "\nPlace your bet and roll the dice (minimum $1 - 'q' to quit): ";
cin >> betChar;
if (betChar == 'q' || betChar == 'Q')
   exit(1);
bet=betChar;

Only this is probably still not what you want, as it will only input a single character, and the line
 
bet=betChar;

will assign the ASCII value of the character to bet.
So a betChar of '1' gives a bet of 49.
To allow an input of more than 1 digit you will need to use a string rather than a char, and you will then need to convert this to an integer if it is not a 'q' or 'Q'.

Since you want to have the same code twice in the program be a reasonable idea to put the bet / quit code into a function.

NB: Use the 'Insert Code' button to create a pair of code tags and put your code between them to help make it more readable on the forum:-)

Topic archived. No new replies allowed.