| | |
Need help with craps program
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2004
Posts: 11
Reputation:
Solved Threads: 0
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
<< moderator edit: fixed [code][/code] tags >>
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![]() |
Similar Threads
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- What's the HARDEST program you've written? (Computer Science)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP)
- Need Help on my Program (Python)
- Program is shutting down right after program is executed (C++)
- 3d Program (Game Development)
Other Threads in the C++ Forum
- Previous Thread: What's wrong with string.replace()?
- Next Thread: allows a user to put a maximum and minimum price and see all properties in that price
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





