By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,905 Members | 2,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,905 IT Pros & Developers. It's quick & easy.

Can I get help with code for game of craps

P: 5
i have this code that plays the game of craps, but i need to be able to take how much they brought and how much they want to bet. show the number on the dice and tell them it they won or lose and what their money is after their roll. and have to ask if they want to play again.

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

const int NUM_GAMES = 10000;

// Function prototypes
int roll_dice();


// ======================
// roll_dice
// Simulates rolling two six-sided dice
// and returns the sum.
// ======================
//roll dice
int dice1, dice2;
int roll_dice()

{
int sum;

dice1=(rand()%6);
dice2=(rand()%6);

sum=(dice1 + dice2 + 2);
//return the sums of the dice
return sum;

}

// ======================
// main function
// ======================
int main()
{
// Variable declarations
int numWins = 0;
int numLosses = 0;
int i;
int roll;
int point;
int sum;

// Initialize random number generator using
// the current value of the clock
srand(time(NULL));
for (i=0; i<NUM_GAMES; i++)
{
roll=roll_dice();

//roll dice
//see what the dice equal
if ((sum == 7) || (sum == 11))
{
numWins++;
}
else if((sum == 2)|| (sum == 3) || (sum == 12))
{
numLosses++;
}
else
//set sum to points
{
point=roll;
do
{
roll=roll_dice();
if (roll==7)
{
numLosses++;
}
else if (roll==point)
{
numWins++;
}
}while ((point != roll) && (roll!=7));
}
}
}



// Output probability of winning
cout << "In the simulation, we won " << numWins <<
" times and lost " << numLosses << " times, " <<
" for a probability of " <<
static_cast<double>(numWins) / (numWins+numLosses) <<
endl << endl;
system("Pause");
return 0;
}
Sep 21 '06 #1
Share this Question
Share on Google+
1 Reply


100+
P: 144
The following are simple examples of reading and writing using cout and cin.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main(int argc, char **argv)
  5. {
  6. int a = 5;
  7.  
  8. // Print value to screen
  9. cout << "The value of \"a\" is : " << a << endl;
  10.  
  11. // Read in new value for a
  12. cout << "Enter an integer : ";
  13. cout.flush();
  14. cin >> a;
  15.  
  16. // Print new value to screen
  17. cout << "The new value of \"a\" is : " << a << endl;
  18.  
  19. return 0;
  20. }
  21.  
Sep 21 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.