I can't for the life of me get this program to run correctly...
It is supposed to simulate a game or games of craps
//Program 2
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int die1;
int die2;
int sum;
int point;
char play;
int played;
int won;
int lost;
played = 0;
won = 0;
lost = 0;
point = 0;
srand( time(NULL) );
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
cout << "die1 = " << die1 << " and die2 = " << die2 << endl;
cout << "sum = " << sum << endl;
point = sum;
if (sum == 7 || sum == 11)
{
cout << "Congratulations. You win!" << endl;
cout << "Play again? Y/N" << endl;
cin >> play;
won++;
}
else if ( sum == 2)
{
cout << "Sorry. You lose." << endl;
cout << "Play again? Y/N" << endl;
cin >> play;
lost++;
}
else
cout << "Your point is: " << sum << endl;
cout << "Press ENTER to roll again." << endl;
std::cin.get();
while(cin)
{
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
cout << "die1 = " << die1 << " and die2 = " << die2 << endl;
cout << "sum = " << sum << endl;
while (sum != point)
{
if (sum == 7)
{
cout << "Sorry. You lose." << endl;
cout << "Play again? Y/N" << endl;
cin >> play;
lost++;
}
else
cout << "Press ENTER to roll again." << endl;
std::cin.get();
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
cout << "die1 = " << die1 << " and die2 = " << die2 << endl;
cout << "sum = " << sum << endl;
}
cout << "Congratulations. You won!" << endl;
won++;
cout << "Play again? Y/N" << endl;
cin >> play;
}
played++;
//***********************************************************************************8
while (play == 'Y')
{
point = 0;
sum = 0;
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
cout << "die1 = " << die1 << " and die2 = " << die2 << endl;
cout << "sum = " << sum << endl;
point = sum;
if (sum == 7 || sum == 11)
{
cout << "Congratulations. You win!" << endl;
cout << "Play again? Y/N" << endl;
cin >> play;
won++;
}
else if ( sum == 2)
{
cout << "Sorry. You lose." << endl;
cout << "Play again? Y/N" << endl;
cin >> play;
lost++;
}
else
cout << "Your point is: " << sum << endl;
cout << "Press ENTER to roll again." << endl;
std::cin.get();
while(cin)
{
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
cout << "die1 = " << die1 << " and die2 = " << die2 << endl;
cout << "sum = " << sum << endl;
while (sum != point)
{
if (sum == 7)
{
cout << "Sorry. You lose." << endl;
cout << "Play again? Y/N" << endl;
cin >> play;
lost++;
}
else
cout << "Press ENTER to roll again." << endl;
std::cin.get();
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
sum = die1 + die2;
cout << "die1 = " << die1 << " and die2 = " << die2 << endl;
cout << "sum = " << sum << endl;
}
cout << "Congratulations. You won!" << endl;
won++;
cout << "Play again? Y/N" << endl;
cin >> play;
}
played++;
}
//*****************************************************88
cout << "Total games played: " << played << endl;
cout << "Total games won: " << won << endl;
cout << "Total games lost: " << lost << endl;
return 0;
}
Last edited on
Add <cstdlib> to the includes because rand and srand is part of that library. Are the error codes printing on your compiler?
and change while(cin)
to while (play !='n') and it may work better, I didn't try it but it should work better.