944,030 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1330
  • C++ RSS
Oct 31st, 2009
0

Game of Crap

Expand Post »
Hello, I am beginner with c++ programming and I need help with this source. I tried to find a way for getPrintDetails to not print a infomation.

//Course: 4002-208
//Author: Joseph Dalpra
//Topic: Functions
//Name: project4.cpp
//Purpose: Using functions to shorten repeated code
// Play the game of craps for a number of games input by user
// Print the results of the game if the user chooses to
// Allow a random seed or the user to input a seed
//----------------
// Include Section
//----------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>



using namespace std;


//---------------------------
// Function Prototype Section
//---------------------------
int roll();
int roll2Dice();
int getNumGames();
int setRand();
bool playCraps(int, bool);
int getPrintDetails();


// ------ M A I N P R O G R A M ------
int main ()
{


int numGames = 0; //number of games to play
int point = 0; //value for the roll of the first 2 dice
int seed = 0; //value for the seed
int sum = roll2Dice();
int numWins = 0; //number of wins
int numLosses = 0; //number of losses
double winPercent = 0.0; //value for the percent of wins
int numLongWins = 0; //value for the longest win streak
int numLongLosses = 0; //value for the longest streak of losses
int nextRoll = roll2Dice(); //sequential roll if no win on first roll
bool print;

//call function setRand to get the seed for the random number generator
seed = setRand();
cout<<endl;

//call function getNumGames to get the number of games to be played
numGames =getNumGames();
cout<<endl;

//call getPrintDetails function
getPrintDetails();





//call craps function
//play craps for while k <= number of games
for (int k=1; k<=numGames; k++)
{
if(playCraps(k,print))
numWins++;
}

//calculate the longest streaks of wins

if (point ==7 || point ==11)
{
numLongWins++;
}
int numlose;

numlose = numGames - numWins;



//calculate the win percentage
winPercent= ((double)numWins/(double)numGames)*100;

//print the percentage of games won
//print longest win streak
//print longest loss streak
cout<<"The number of games won out of "<< numGames << " is " << numWins <<endl;
cout << "the percent of games won " << fixed << setprecision(4)<< winPercent <<endl;
cout<<"The number of games lose out of "<< numGames << " is " << numlose <<endl;
//cout<<"Longest run of wins is " << numLongWins <<endl;
cout<<"Longest run of losses is " << numLongLosses <<endl;


//freeze the window
//halt program

system ("Pause");
return 0;

}


//Name:roll
//Purpose: generate a random number 1-6 to simulate a die roll
//
//Parameters: none
//Return: generate a random number 1 - 6 inclusively
//---------------------------------------------------------------
int roll(void)
{
return ( 1 + rand()%6);
}

//Name:roll2Dice
//Purpose: roll 2 dice and add them together to give the player their point
// return value of 2 die rolls
//Parameters: none
//Return: sum
//---------------------------------------------------------------
int roll2Dice(void)
{
int sum = roll() + roll();
return sum;
}

//Name:getNumGames
//Purpose: prompt the user to input the number of games
// validate the input to make sure it is >0
//
//Parameters: none
//Return: numGames
//---------------------------------------------------------------
int getNumGames()
{
int numGames = 0; //number of games to be played
cout<<"Enter number of games to be played: ";
cin>>numGames;
while (numGames <=0)
{
cout<<"Invalid number of games - must be > 1"<<endl;
cout<<"Enter number of games to be played: ";
cin>>numGames;
}
return numGames;
}
//Name:setRand
//Purpose: prompt user for input of a seed
// 1-50 start the seed at input value
// 0 is a random seed
//Parameters: none
//Return: none
//---------------------------------------------------------------
int setRand()
{
int seed; //value for seed for random generator
cout<<"Enter the seed 1..50 or 0 for random seed: ";
cin>>seed;
if (seed == 0)
{
srand(seed);

}
while (seed <0 || seed > 50)
{
cout<<"Invalid seed - must be 0..50"<<endl;
cout<<"Enter seed: ";
cin>>seed;
}
srand(seed);
}

//Name:getPrintDetails
//Purpose:
//
//
//Parameters: none
//Return: none
//---------------------------------------------------------------
int getPrintDetails()
{
int gameNum;
int point;
bool print = ' ';
cout<<"Do you wish to print details <Y/N>?: ";
cin>>print;
print=toupper(print);

if (print == 'Y')
{
return true;
}
else
{
return false;
}
}
//---------------------------------------------------------------
//Name:playCraps
//Purpose: play a game of craps
//
//
//Parameters: none
//Return: none
//---------------------------------------------------------------

bool playCraps(int gameNum, bool print)
{
int point = roll2Dice();
int nextRoll;
int numLosses = 0;
int numWins = 0;



cout << "Game " << gameNum << ": " << point;
if (point ==2 || point==3 ||point==12)
{

cout<<" loss "<<endl;
return false;

}
else if (point ==7 || point ==11)
{
cout<<" win "<<endl;
return true;

}
else
{
do
{
nextRoll=roll2Dice();
cout<<" " << nextRoll;

}while(nextRoll !=7 && nextRoll !=point);
if(nextRoll == 7)
{
cout<<" loss "<<endl;
return false;

}
else
{
cout<<" win "<<endl;
return true;

}
}

return print;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
freakgamerz is offline Offline
3 posts
since Oct 2009
Oct 31st, 2009
0
Re: Game of Crap
C++ Syntax (Toggle Plain Text)
  1. int getPrintDetails() {
  2. int gameNum;
  3. int point;
  4. char print = 0;
  5. cout<<"Do you wish to print details <Y/N>?: ";
  6. cin >> print;
  7. return (print == 'Y');
  8. }
Reputation Points: 10
Solved Threads: 10
Light Poster
niXman is offline Offline
39 posts
since Oct 2009
Oct 31st, 2009
0
Re: Game of Crap
Please format code. Read impossible
And insert in the tags.
Reputation Points: 10
Solved Threads: 10
Light Poster
niXman is offline Offline
39 posts
since Oct 2009
Oct 31st, 2009
0
Re: Game of Crap
sorry it is not working
Reputation Points: 10
Solved Threads: 0
Newbie Poster
freakgamerz is offline Offline
3 posts
since Oct 2009
Oct 31st, 2009
0
Re: Game of Crap
This function "getPrintDetails ()" - works. Perhaps it is in something?
Describe the problem ...
Reputation Points: 10
Solved Threads: 10
Light Poster
niXman is offline Offline
39 posts
since Oct 2009
Oct 31st, 2009
0
Re: Game of Crap
I need to print details by response Y (yes) while not print by N (no) I think it will do something with playCraps function
Reputation Points: 10
Solved Threads: 0
Newbie Poster
freakgamerz is offline Offline
3 posts
since Oct 2009
Oct 31st, 2009
0
Re: Game of Crap
If I understand you correctly, something like this:
C++ Syntax (Toggle Plain Text)
  1. int getPrintDetails() {
  2. int gameNum = 0;
  3. int point = 0;
  4. char print = 0;
  5. cout<<"Do you wish to print details <Y/N>?: ";
  6. cin >> print;
  7. return playCraps(gameNum , print == 'Y');
  8. }
Reputation Points: 10
Solved Threads: 10
Light Poster
niXman is offline Offline
39 posts
since Oct 2009
Nov 1st, 2009
0
Re: Game of Crap
I just want to say that I love the title of this thread and I hope it stays at the top for awhile.
Reputation Points: 237
Solved Threads: 117
Practically a Posting Shark
Clinton Portis is offline Offline
822 posts
since Oct 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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:
Previous Thread in C++ Forum Timeline: Need some help with if statements
Next Thread in C++ Forum Timeline: How do I find the total sale for my program??





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


Follow us on Twitter


© 2011 DaniWeb® LLC