943,972 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 5582
  • C++ RSS
Apr 26th, 2005
0

Need help with craps program

Expand Post »
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 >>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ellas747 is offline Offline
11 posts
since Sep 2004
Apr 26th, 2005
0

Re: Need help with craps program

What problems are you having with it?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
marinme is offline Offline
63 posts
since Apr 2005
Apr 27th, 2005
0

Re: Need help with craps program

the bold parts. i need code
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ellas747 is offline Offline
11 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC