Need help figuring out the last problem to a craps game
Apr 13, 2012 at 5:24am UTC
Hello,
I am in a beginners C++ course and have to complete a craps game assignment. The only specific requirement is to use a void function to print out a banner whenever a user does a.out. I don't have any compiling errors, but the game goes into an infinite if loop only when the player rolls a point. I have been at this since yesterday but can't figure it out. Ill post the current code and then the previous.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#include <iostream>
#include <cstdlib> //prototype for srand and rand
#include <ctime>
using namespace std;
void printBanner();
int main()
{
int die1, die2, sum, sum2, point;
srand(time(0));
char Continue = 'Y' ;
printBanner();
cout << "\nPress Enter to roll the dice." << endl;
while (Continue == 'Y' )
{
cin.get();
die1 = (rand() % 6) + 1;
die2 = (rand() % 6) + 1;
sum = die1 + die2;
cout << "You rolled: " << die1 << " plus " ;
cout << die2 << " = " << sum << endl;
if ((sum==7) || (sum==11))
cout << "Congratulations, you won!" << endl;
else if ((sum==2) || (sum==3) || (sum==12))
cout <<"You lose, better luck next time." << endl;
else if ((sum==4) || (sum==5) || (sum==6) || (sum==8) || (sum==10)
|| (sum == 9))
{
point=sum;
cout << "Your point is " << point << endl;
}
do
{
cout << "\nPress Enter to roll the "
<< "dice." << endl;
cin.get();
die1 = (rand() % 6) + 1;
die2 = (rand() % 6) + 1;
sum2 = die1 + die2;
cout << "You rolled: " << die1 << " plus " ;
cout << die2 << " = " << sum2 << endl;
if (sum2==point)
cout << "Congratulations, you won!" << endl;
else if (sum2==7)
cout << "You lose, better luck next time!" << endl;
}
while ((sum2 != point) || (sum2 != 7));
cout << "\nWant to play again?" << endl;
cout << "(Y)es or (N)o: " ;
cin >> Continue;
cout << endl;
Continue = toupper (Continue);
}
cout << "The game will now end." << endl;
return 0;
}
void printBanner()
{
cout << "\n******************************"
<< "****************************" << endl;
cout << "********* Welcome to the Name's "
<< "Casino ******" << endl;
cout << "********* Step up to the table and "
<< "place your bets! ******" << endl;
cout << "******************************"
<< "****************************" << endl;
}
And the previous:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
#include <iostream>
#include <cstdlib> //prototype for srand and rand
#include <ctime>
using namespace std;
void printBanner();
int main()
{
int die1, die2, sum, sum2, point;
srand(time(0));
char Continue = 'Y' ;
printBanner();
cout << "\nPress Enter to roll the dice." << endl <<endl;
while (Continue == 'Y' )
{
cin.get();
die1 = (rand() % 6) + 1;
die2 = (rand() % 6) + 1;
sum = die1 + die2;
cout << "You rolled: " << die1 << " plus " ;
cout << die2 << " = " << sum << endl;
if ((sum==7) || (sum==11))
cout << "\nCongratulations, you won!" << endl;
else if ((sum==2) || (sum==3) || (sum==12))
cout <<"\nYou lose, better luck next time." << endl;
else if ((sum==4) || (sum==5) || (sum==6) || (sum==8) || (sum==10)
|| (sum == 9))
{
point=sum;
cout << "\nYour point is " << point << endl;
do
{
cout << "\nPress Enter to roll the "
<< "dice." << endl;
cin.get();
die1 = (rand() % 6) + 1;
die2 = (rand() % 6) + 1;
sum2 = die1 + die2;
cout << "You rolled: " << die1 << " plus " ;
cout << die2 << " = " << sum2 << endl;
if (sum2==point)
cout << "\nCongratulations, you won!" << endl;
else if (sum2==7)
cout << "\nYou lose, better luck next time!" << endl;
}
while ((sum2 != point) || (sum2 != 7));
}
cout << "Want to play again?" << endl;
cout << "(Y)es or (N)o: " ;
cin >> Continue;
cout << endl;
Continue = toupper (Continue);
}
cout << "The game will now end." << endl;
return 0;
}
void printBanner()
{
cout << "******************************"
<< "****************************" << endl;
cout << "********* Welcome to the Name's "
<< "Casino ******" << endl;
cout << "********* Step up to the table and "
<< "place your bets! ******" << endl;
cout << "******************************"
<< "****************************" << endl;
}
Please help!
Last edited on Apr 15, 2012 at 7:56am UTC
Apr 13, 2012 at 5:45am UTC
1 2
cout << "Your roll: " << die1 << " plus " //You cout die1 twice, instead of die1 and then die2
<< die1 << " = " << roll << endl;
Topic archived. No new replies allowed.