I have looked around in a lot of forums and have seen many examples of this game. However, mine is slightly different than the rest.
I am having trouble with a function called "getPrintDetails()". It has no parameters and is supposed to return "true" if the game details are to be printed and "false" if they are not. The details are always printed how I have my function written currently. I am pretty sure this is because I have the cout statements in my function for craps. However, I am not sure how to put them into the getPrintDetails function.
An example of the details is
Game 1: 4 6 2 10 4 win
Game 2: 6 2 10 7 loss
etc.
I am also having trouble calculating the longest number of wins, and the longest number of losses. I have no idea how to go about this.
I am sorry that this is a common repost but this is my first programming class in college and am slightly confused.
Any help is greatly appreciated.
Thanks,
Steve
//Course: 4002-208
//Author: Stephen Damon
//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>
using std::cout;
using std::endl;
using std::cin;
#include <iomanip>
using std::setprecision;
using std::fixed;
#include <cstdlib>
using std::rand;
using std::srand;
#include <ctime>
using std::time;
//---------------------------
// 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++;
}
//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 <<" for a winning percentage of " << fixed << setprecision(4)<< winPercent <<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-100 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..100 or 0 for random seed: ";
cin>>seed;
if (seed == 0)
{
srand(seed);
}
while (seed <0 || seed > 100)
{
cout<<"Invalid seed - must be 0..100"<<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;
//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;
//call getPrintDetails function
getPrintDetails();
but getPrintDetails() is an int function. After it ask u if u want to print details it returns true or false, but never verify witch is the answer.
U might do something like:
I dont know of this will solve the whole problem (becaus i dont understand the whole problem), but you're using type int for getPrintDetails() while that function returns a bool-value.
Besides that, i dont understand how you want to use this function. You just call it, without using it as an condition, and the function itself don't change any global variables or someting. (I havent looked at the whole code, so if i missing someting...)
Our getPrintDetails function is to prompt the user to enter whether the detail information for every game is to be printed. The function is supposed to have no parameters and return true if the details are to be printed and false if they are not to be printed.
@stevedye56
Yes, i understood that already, but you're not using the function. You're only calling it onces, right? You call the function, the function does what is has to do, returns either true or false, but because you're not using the function as condition or someting, that true/false disappear and dont have any effect of the rest of the program
Well....I think this are the details u're asking about....
//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 <<" for a winning percentage of " << fixed << setprecision(4)<< winPercent <<endl;
cout<<"Longest run of wins is " << numLongWins <<endl;
cout<<"Longest run of losses is " << numLongLosses <<endl;
in main(); Every think in this quote must be in an if bloc.
if(getPrintDetails) {
//print
} else {
//don't or whatever
}
or like firedraco said, but that involves the presence of a new var.
I hope I understood right witch are those details u were talking about.
Regards!