-
Craps - stuck
Hello All,
I've read almost all of the other Craps programs online, but I'm stuck on mine and would appreciate any help. My problem is somewhere in one of my loops. (Please bear with me, I'm a newbie and this is my first post, sorry if its sloppy. Any and all comments are appreciated as well).
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
using namespace std;
int main()
{
int betAmount;
string answer;
cout << "*** Welcome to HIGH ROLLER Craps *** \n"; // Telling the user what the program is.
cout << "________________________________________\n";
cout << "Ladies and Gentlemen....Place your bets! \n\n";
cin >> betAmount;
cout << "\n You bet $" << betAmount << ". \n\n";
int dice1, dice2;
dice1 = rand(); // These are the random dice rolls.
dice2 = rand();
unsigned seed; // Random generator seed.
seed = time(0);
srand(seed); // Initialize random seed.
char again; // Loop again? Yes or no?
do
{
dice1 = 1 + rand() % 6; // This generates a random number for dice 1
dice2 = 1 + rand() % 6; // This generates a random number for dice 2
cout << dice1 << " " << dice2 << " " << endl << endl;
double tod = dice1 + dice2; // This is total of the dice
combined
cout << "The total of the dice is " << tod << endl << endl;
{
double pointTod = tod;
if (tod == 2 || tod == 3 || tod == 12)
{
cout << "You LOST $" << betAmount << endl << endl;
cout << " Want to play again? (Y/N) \n\n";
cin >> again;
}
else if (tod == 7 || tod == 11)
{
cout << "You WON $" << betAmount * 2 << endl << endl;
cout << " Want to play again? (Y/N) \n\n";
cin >> again;
}
else if (tod == 4 || tod == 5 || tod == 6 || tod == 8 || tod
== 9 || tod == 10)
{
cout << "Bet rides, your point is " << tod << " *ROLL
AGAIN* (press 'y') \n\n";
cin >> again;
}
{
dice1 = 1 + rand() % 6;
dice2 = 1 + rand() % 6;
cout << dice1 << " " << dice2 << " " << endl <<
endl;
tod = dice1 + dice2;
cout << "The total of the dice is " << tod << endl <<
endl;
cout << "Bet rides, ROLL AGAIN (press 'y') \n\n";
}
{if (pointTod)
{
cout << "The total of the dice is " << pointTod <<
endl << endl;
cout << "You won!!!\n\n";
cin >> again;
}
else if (tod == 7 || tod == 11)
{
cout << "You lose!\n\n";
cout << " Want to play again? (Y/N) \n\n";
cin >> again;
}
else if (tod == 2 || tod == 3 || tod == 4 || tod == 5 ||
tod == 6 || tod == 8 || tod == 9 || tod == 10 ||
tod == 12)
{
cout << "Bet rides, ROLL AGAIN (press 'y') \n\n";
cin >> again;
dice1 = 1 + rand() % 6;
dice2 = 1 + rand() % 6;
cout << dice1 << " " << dice2 << " " << endl
<< endl;
tod = dice1 + dice2;
cout << "The total of the dice is " << tod << endl
<< endl;
}
}
}
}
while ((again == 'y') || (again == 'n'));
system("pause");
}
-
what error appears , compile error or logical
i have compiled the next code and i think it works
if the problem does not have to do with compile then please tell us what make you think its wrong
what i noticed is that the while keep looping even if the user says n because of this
Code:
while ((again == 'y') || (again == 'n'));
i think you meant this
Code:
while ((again == 'y')
also this
Code:
{
dice1 = 1 + rand() % 6;
dice2 = 1 + rand() % 6;
cout << dice1 << " " << dice2 << " " << endl <<
endl;
tod = dice1 + dice2;
cout << "The total of the dice is " << tod << endl <<
endl;
cout << "Bet rides, ROLL AGAIN (press 'y') \n\n";
}
always run because its not inside of any if
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
using namespace std;
int main()
{
int betAmount;
string answer;
cout << "*** Welcome to HIGH ROLLER Craps *** \n"; // Telling the user what the program is.
cout << "________________________________________\n";
cout << "Ladies and Gentlemen....Place your bets! \n\n";
cin >> betAmount;
cout << "\n You bet $" << betAmount << ". \n\n";
int dice1, dice2;
dice1 = rand(); // These are the random dice rolls.
dice2 = rand();
unsigned seed; // Random generator seed.
seed = time(0);
srand(seed); // Initialize random seed.
char again; // Loop again? Yes or no?
do
{
dice1 = 1 + rand() % 6; // This generates a random number for dice 1
dice2 = 1 + rand() % 6; // This generates a random number for dice 2
cout << dice1 << " " << dice2 << " " << endl << endl;
double tod = dice1 + dice2; // This is total of the dice
cout << "The total of the dice is " << tod << endl << endl;
{
double pointTod = tod;
if (tod == 2 || tod == 3 || tod == 12)
{
cout << "You LOST $" << betAmount << endl << endl;
cout << " Want to play again? (Y/N) \n\n";
cin >> again;
}
else if (tod == 7 || tod == 11)
{
cout << "You WON $" << betAmount * 2 << endl << endl;
cout << " Want to play again? (Y/N) \n\n";
cin >> again;
}
else if (tod == 4 || tod == 5 || tod == 6 || tod == 8 || tod
== 9 || tod == 10)
{
cout << "Bet rides, your point is " << tod << "*ROLL AGAIN* (press 'y') \n\n";
cin >> again;
}
{
dice1 = 1 + rand() % 6;
dice2 = 1 + rand() % 6;
cout << dice1 << " " << dice2 << " " << endl <<
endl;
tod = dice1 + dice2;
cout << "The total of the dice is " << tod << endl <<
endl;
cout << "Bet rides, ROLL AGAIN (press 'y') \n\n";
}
{if (pointTod)
{
cout << "The total of the dice is " << pointTod <<
endl << endl;
cout << "You won!!!\n\n";
cin >> again;
}
else if (tod == 7 || tod == 11)
{
cout << "You lose!\n\n";
cout << " Want to play again? (Y/N) \n\n";
cin >> again;
}
else if (tod == 2 || tod == 3 || tod == 4 || tod == 5 ||
tod == 6 || tod == 8 || tod == 9 || tod == 10 ||
tod == 12)
{
cout << "Bet rides, ROLL AGAIN (press 'y') \n\n";
cin >> again;
dice1 = 1 + rand() % 6;
dice2 = 1 + rand() % 6;
cout << dice1 << " " << dice2 << " " << endl
<< endl;
tod = dice1 + dice2;
cout << "The total of the dice is " << tod << endl
<< endl;
}
}
}
}
while ((again == 'y') || (again == 'n'));
system("pause");
}
-
Sorry cable, my bad. I should have explained better. The program compiles but when I run it, I get the following type of undesired output:
http://cboard.cprogramming.com/image...AAAElFTkSuQmCC
As you can see, it appears the '3' stayed in memory and was saved as the 'point' value, even though it was the first roll and came up as a lose. '4' should have become the point because it was supposed to start a new game after the '3' lost. On the first roll of the new game, '4' should have become the point. I hope I didn't confuse you, because I know I am.
-
Dang, I hate being an inexperienced newbie. The image I copied in was too big. Anyway, the output was:
Code:
***Welcome to HIGH ROLLER Craps ***
_________________________________
Ladies and Gentlemen....Place your bets!
5
You bet $5.
2 1
The total of the dice is 3
You LOST $5
Want to play again? (Y/N)
y
3 1
The total of the dice is 4
Bet rides, ROLL AGAIN (press 'y')
The total of the dice is 3
You won!!!
-
compile this and tell me if its correct
what i changed is listed at my previous post
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <time.h>
#include <iomanip>
using namespace std;
int main()
{
int betAmount;
string answer;
cout << "*** Welcome to HIGH ROLLER Craps *** \n"; // Telling the user what the program is.
cout << "________________________________________\n";
cout << "Ladies and Gentlemen....Place your bets! \n\n";
cin >> betAmount;
cout << "\n You bet $" << betAmount << ". \n\n";
int dice1, dice2;
dice1 = rand(); // These are the random dice rolls.
dice2 = rand();
unsigned seed; // Random generator seed.
seed = time(0);
srand(seed); // Initialize random seed.
char again; // Loop again? Yes or no?
do
{
dice1 = 1 + rand() % 6; // This generates a random number for dice 1
dice2 = 1 + rand() % 6; // This generates a random number for dice 2
cout << dice1 << " " << dice2 << " " << endl << endl;
double tod = dice1 + dice2; // This is total of the dice
cout << "The total of the dice is " << tod << endl << endl;
{
double pointTod = tod;
if (tod == 2 || tod == 3 || tod == 12)
{
cout << "You LOST $" << betAmount << endl << endl;
cout << " Want to play again? (Y/N) \n\n";
cin >> again;
}
else if (tod == 7 || tod == 11)
{
cout << "You WON $" << betAmount * 2 << endl << endl;
cout << " Want to play again? (Y/N) \n\n";
cin >> again;
}
else if (tod == 4 || tod == 5 || tod == 6 || tod == 8 || tod
== 9 || tod == 10)
{
cout << "Bet rides, your point is " << tod << "*ROLL AGAIN* (press 'y') \n\n";
cin >> again;
dice1 = 1 + rand() % 6;
dice2 = 1 + rand() % 6;
cout << dice1 << " " << dice2 << " " << endl <<
endl;
tod = dice1 + dice2;
cout << "The total of the dice is " << tod << endl <<
endl;
cout << "Bet rides, ROLL AGAIN (press 'y') \n\n";
}
else if(tod == 7 || tod == 11)
{
cout << "You lose!\n\n";
cout << " Want to play again? (Y/N) \n\n";
cin >> again;
}
else if (tod == 2 || tod == 3 || tod == 4 || tod == 5 ||
tod == 6 || tod == 8 || tod == 9 || tod == 10 ||
tod == 12)
{
cout << "Bet rides, ROLL AGAIN (press 'y') \n\n";
cin >> again;
dice1 = 1 + rand() % 6;
dice2 = 1 + rand() % 6;
cout << dice1 << " " << dice2 << " " << endl
<< endl;
tod = dice1 + dice2;
cout << "The total of the dice is " << tod << endl
<< endl;
}
}
}
while (again == 'y');
system("pause");
}
-
What exactly is this program for?
-
Virtual Ace, this program is the game of Craps, which is a dice game.
Cable, sorry for the delay in responding, had to go to work.
When I compile this, obviously I get different results depending on how the dice come out. The program runs correctly if you win with a 7 or 11 on the first roll, or if you lose with a 2, 3, or 12. My problem lies with if a point is made on the first roll. If a 7 or 11 comes out after the point is made, it says I won. It should say I lose. The problem appears to be in the loop after the point is made, but I cannot figure out exactly where that is. I’ve tried several different combination but come up with the same undesired result.