-
Help with Craps game
Can anyone help me out on this thing. This is my first assignment and can't figure it out. But I can't get out of the loop after I roll the point value or roll a 7 to lose after the first roll. The game will just keep going. Also I need to a way to error trap at the end when the game asks if you want to continue. I have to loop that to where if the user enters something other than Y or N then it will repeat. But if its a N then you get the good bye message and exit. If the user enters Y, then the game starts over. Thanks for the help!
Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void main()
{
srand((unsigned)time(NULL));
int firstRoll, nextRoll, die1, die2;
char playAgain;
cout << "Welcome to Craps!" << endl << endl;
do
{
cout << "The game is about to begin..." << endl;
system("pause");
cout << endl;
die1 = (rand() % 6 + 1);
die2 = (rand() % 6 + 1);
firstRoll = die1 + die2;
cout << "You rolled a " << die1 << " and a " << die2 << " for a total of " << firstRoll << "." << endl;
if (firstRoll == 7 || firstRoll == 11)
{
cout << "Congratulations, you win!" << endl;
}
else if ((firstRoll == 2) || (firstRoll == 3) || (firstRoll == 12))
{
cout << "Sorry, you lose" << endl;
}
else
{
do
{
cout << "Your point value is: " << firstRoll << endl;
system("pause");
cout << endl;
die1 = (rand() % 6 + 1);
die2 = (rand() % 6 + 1);
nextRoll = die1 + die2;
cout << "You rolled a " << die1 << " and a " << die2 << " for a total of " << nextRoll << "." << endl;
if (nextRoll == firstRoll)
{
cout << "Congratulations, you rolled your point value. YOU WIN!" << endl << endl;
}
else if (nextRoll == 7)
{
cout << "Sorry, you lost." << endl;
}
else
{
cout << "Please roll again." << endl;
system("pause");
cout << endl;
}
} while ((nextRoll != 7) || (nextRoll != firstRoll));
}
cout << "Would you like to play again (Y or N)? " << endl;
cin >> playAgain;
if ((playAgain == 'n') || (playAgain == 'N'))
{
cout << "Thanks for playing!" << endl;
system("pause");
}
else if ((playAgain == 'y') || (playAgain == 'Y'))
{
}
else
{
cout << "Please enter either Y or N!" << endl;
}
//}while;
}while ((playAgain == 'y') || (playAgain == 'Y'));
}// end of main
-
Code:
} while ((nextRoll != 7) || (nextRoll != firstRoll));
should be
Code:
} while ((nextRoll != 7) && (nextRoll != firstRoll));
You want to continue the loop only if the roll is not seven AND the roll is not the same as the previous roll.
Also, did you see the thread that said "<< !! Posting Code? Read this First !! >>" ? You posted code, so you should have probably read that first. It says use code tags... it would have been easier to see the error with them. You can edit your post and add the code tags.
P.S. Congratulations on using the standard library headers... many new posters use the old ones. However, what's with void main()? Even though some compilers accept it, that has never been valid in C++. It should be int main(). If your instructor says to use void main, point them to this link (http://www.research.att.com/~bs/bs_faq2.html#void-main) and ask why they disagree with the creator of the language. ;)
And it looks like Salem's fixed the Code tags for you.