I'm doing some program about the craps game(rolling dice)
and it seems just confusing that whenever I prompt the user for input of the number of times of the crap games they want to play. The program will always run at the first choice that I give(see below). Is something about syntax or some hidden magic going wrong? I'm exhausted as a beginner...
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
unsignedint rollDice(); // function prototype
int main()
{
int selection;
cout << "Please make your selection: " << endl;
cout << "1). Play 1,000 craps games." << endl;
cout << "2). Play your own modified times of craps games."<< endl;
cout << "3). Show answers to \n What are the chances of winning at craps?\n"
<< " Do the chances of winning improve with the length of the game?" << endl;
cout << "4). Exit." << endl;
cin >> selection;
if (selection = 1)
{
for (int x = 0; x < 1000; x++)
{
enum Status { CONTINUE, WON, LOST };
unsignedint myPoint = 0;
Status gameStatus = CONTINUE;
unsignedint sumOfDice = rollDice();
switch (sumOfDice)
{
case 7:
case 11:
gameStatus = WON;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
cout << "Point is " << myPoint << endl;
break;
}
while (CONTINUE == gameStatus)
{
sumOfDice = rollDice();
if (sumOfDice == myPoint)
gameStatus = WON;
elseif (sumOfDice == 7)
gameStatus = LOST;
}
if (WON == gameStatus)
cout << "Player Wins!!!" << endl;
else
cout << "Player Loses..." << endl;
}
}
if (selection = 2)
{
int fnum;
cout << "Choose the times that you want the game to be played: " << endl;
cin >> fnum;
for (int x = 0; x < fnum; x++)
{
enum Status { CONTINUE, WON, LOST };
unsignedint myPoint = 0;
Status gameStatus = CONTINUE;
unsignedint sumOfDice = rollDice();
switch (sumOfDice)
{
case 7:
case 11:
gameStatus = WON;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
cout << "Point is " << myPoint << endl;
break;
}
while (CONTINUE == gameStatus)
{
sumOfDice = rollDice();
if (sumOfDice == myPoint)
gameStatus = WON;
elseif (sumOfDice == 7)
gameStatus = LOST;
}
if (WON == gameStatus)
cout << "Player Wins!!!" << endl;
else
cout << "Player Loses..." << endl;
}
}
//if (selection = num3)
//
//if (selection = num4)
//
/*enum Status { CONTINUE, WON, LOST };
srand(static_cast<unsigned int>(time(0)));
unsigned int myPoint = 0;
Status gameStatus = CONTINUE;
unsigned int sumOfDice = rollDice();
switch (sumOfDice)
{
case 7:
case 11:
gameStatus = WON;
break;
case 2:
case 3:
case 12:
gameStatus = LOST;
break;
default:
gameStatus = CONTINUE;
myPoint = sumOfDice;
cout << "Point is " << myPoint << endl;
break;
}
while (CONTINUE == gameStatus)
{
sumOfDice = rollDice();
if (sumOfDice == myPoint)
gameStatus = WON;
else
if (sumOfDice == 7)
gameStatus = LOST;
}
if (WON == gameStatus)
cout << "Player Wins!!!" << endl;
else
cout << "Player Loses..." << endl;
*/
getchar();
getchar();
getchar();
}
unsignedint rollDice()
{
unsignedint die1 = 1 + rand() % 6;
unsignedint die2 = 1 + rand() % 6;
unsignedint sum = die1 + die2;
cout << "Players Rolled " << die1 << " + " << die2 << " = " << sum << endl;
return sum;
}
so after the if selection = 2; the program will run 1000 times first and then ask for the number. Can anybody help... This may just too easy for you guys but I'm now in no mood of...solving it...
note that when I tried to set the first if condition as comments and run the program. It is doing fine. It will go straight and ask you about the input. What the hell is wrong...