DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   Need help with craps program (http://www.daniweb.com/forums/thread22612.html)

ellas747 Apr 26th, 2005 9:23 pm
Need help with craps program
 
I have this code that I started on but cant seem to finish it. i have to create an file and put the outcomes in it. I cant seem to figure it out. can soemone please help.
thanks,
john
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>

using std::cout;
using std::cerr;
using std::cin;
using std::ios;
using std::endl;
using std::ofstream;
using std::ifstream;

void playCraps();
void reviewStatistics();
int rollDice();

int main()
{
  int choice;
 
  // continue game unless user chooses to quit
  do {
 
      // offer game options
      cout << "Choose an option" << endl
          << "1. Play a game of craps" << endl
          << "2. Review cumulative craps statistics" << endl
          << "3. Quit program" << endl;
         
      cin >> choice;
       
      if ( choice == 1 )
        playCraps();
      else if ( choice == 2 )
        reviewStatistics();

  } while ( choice != 3 );
 
  return 0;

} // end main

// review cumulative craps statistics
void reviewStatistics()
{
   /* Write a body for reviewStatistics which displays
      the total number of wins, losses and die rolls recorded
      in craps.dat */


} // end function reviewStatistics
   
// play game
void playCraps()
{
  enum Status { CONTINUE, WON, LOST };
  int sum;
  int myPoint;
  int rollCount = 0;
  Status gameStatus;
 
  ofstream outCrapfile("Craps.dat", ios::out);
 
  // seed random number generator and roll dice
  srand( time( 0 ) );
  sum = rollDice();
  rollCount++;
 
  // check game conditions
  switch( sum ) {
      case 7:
      case 11:
        gameStatus = WON;
        break;
      case 2:
      case 3:
      case 12:
        gameStatus = LOST;
        break;
      default:
        gameStatus = CONTINUE;
        myPoint = sum;
        cout << "Point is " << myPoint << endl;
        break;

  } // end switch
 
  // keep rolling until player matches point or loses
  while ( gameStatus == CONTINUE ) {
      sum = rollDice();
      rollCount++;
     
      if ( sum == myPoint )
        gameStatus = WON;

      else
        if ( sum == 7 )
            gameStatus = LOST;

  } // end while
 
  //  display status message and write results to file
  if ( gameStatus == WON ) {
      cout << "Player wins\n" << endl;
      /* Write player WIN status and the total number of die
        rolls to a file */                 

  } // end if
  else {
      cout << "Player loses\n" << endl;
      /* Write player LOSE status and the total number of die
        rolls to a file */


  } // end else

} // end function playCraps

// dice rolling function
int rollDice()
{
  int die1;
  int die2;
  int workSum;
 
  // roll two dice
  die1 = 1 + rand() % 6;
  die2 = 1 + rand() % 6;
 
  // total and print results
  workSum = die1 + die2;
  cout << "Player rolled " << die1 << " + " << die2
        << " = " << workSum << endl;
       
  return workSum;

} // end function rollDice
<< moderator edit: fixed [code][/code] tags >>

marinme Apr 26th, 2005 11:56 pm
Re: Need help with craps program
 
What problems are you having with it?

ellas747 Apr 27th, 2005 1:22 pm
Re: Need help with craps program
 
the bold parts. i need code


All times are GMT -4. The time now is 1:22 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC