I'm sure this is something simple I'm just not seeing. Any and all help is greatly appreciated. The endless loop is in the last function called playGame.
//----------------
// Include Section
//----------------
#include <iostream>
#include <string>
#include <iomanip>
#include <math.h>
#include <stdlib.h> //contains description of srand function
#include <time.h>
usingnamespace std;
//function prototypes
int readIntPos(string text);
void setRand(int getSeed);
int roll();
int roll2();
int playGame();
// Main Program
int main()
{
// Constants
constint GAMESIZE = 10000; // 10000 games.
// Variable
double averageRoll = 0; // Average number of rolls for games.
int game[GAMESIZE]; // array for 10000 games.
int getSeed = 0; // seed value for random number generator
int sumRoll = 0; // sum of roll of 2 dice
int minRoll = 0; // min roll for games
int maxRoll = 0; // Max num of rolls for games.
int sum = 0;
int sumgames = 0;
int gameaverage = 0;
int x = 0;
// Get Seed
getSeed = readIntPos("Enter seed for random number generator (999 to use time): ");
setRand(getSeed);
cout << endl;
// Play the game 10000 times
do
{
playGame();
x++;
}while (x <= 10000);
// gathers sum for average calc
for (int n = 0; n < GAMESIZE; n++ )
{
sumgames += game[n];
}
// calcs average
gameaverage = sumgames / static_cast <double> (GAMESIZE);
// calcs max number of rolls
maxRoll = GAMESIZE;
for(int n =0; n < GAMESIZE; n++)
{
if (game[n] > maxRoll)
maxRoll = game[n];
}
cout << "Max roll is : " << maxRoll << endl << endl ;
// calcs min number of rolls
minRoll = GAMESIZE;
for(int n =0; n < GAMESIZE; n++)
{
if (game[n] < minRoll)
minRoll = game[n];
}
cout << "min roll is : " << game << endl << endl ;
return(0);
}
//---------------------------------------------------------------
//Name: readIntPos
//Purpose: reads a valid positive integer value
//Parameters: user prompt of type string
//Returns: valid positive integer
//---------------------------------------------------------------
int readIntPos(string text)
{
int number ;
cout << text;
cin >> number;
while(!(number > 0))
{
cout << "\tInvalid input - must be positive" << endl;
cout << text;
cin >> number;
}
return number;
}
//------------------------------------------
//Name: setRand
//Purpose: seeds the random number generator
//Parameter: seed of the random number entered
//Return: void
//------------------------------------------
void setRand(int getSeed)
{
if(getSeed == 999)
{
srand(time(0));
}
else
{
srand(getSeed);
}
}
//------------------------------------------
//Name: roll
//Purpose: simulate the roll of one die
//Parameter: none
//Return: roll of a die 1 and 6, inclusively
//------------------------------------------
int roll()
{
return 1 + rand() % 6;
}
//-------------------------------------------
//Function: roll2Dice
//Purpose: Simulate the roll of two dice
//Parameters: none
//Return: sum of two die rolls
//-------------------------------------------
int roll2(void)
{
int roll1 = roll();
int roll2 = roll();
return (roll1 + roll2);
}
//-------------------------------------------
//Function: PlayGame
//Purpose: Plays one game of Chutes and Ladders.
//Parameters: none
//Return: returns the number required to land on square 100.
//-------------------------------------------
int playGame()
{
int getSeed = 0; // seed value for random number generator
int sumRoll = 0; // sum of roll of 2 dice
int sum = 0;
int x = 0;
do
{
sum++;
x = roll();
sumRoll = sumRoll + x;
if(sumRoll > 100)
{
sumRoll = sumRoll - x;
}
cout << sum << " ";
cout << endl;
}while(sumRoll < 100);
while (sumRoll < 100)
{
}
}
In this code, any time that sumRoll is increased to over 100 ( sumRoll = sumRoll + x;), you immediately reduce it back under 100 (sumRoll = sumRoll - x;), so it will never ever at the end of the loop be over 100.
The idea behind that is it cannot go over 100, but it can equal 100. So I thought by sumRoll = sumRoll + x; would continue to increment it unless it got over 100 which sumRoll = sumRoll -x would be able to push it back under 100 until it equaled exactly 100.