### Endless loop problem

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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214`` `````` //---------------- // Include Section //---------------- #include #include #include #include #include //contains description of srand function #include using namespace std; //function prototypes int readIntPos(string text); void setRand(int getSeed); int roll(); int roll2(); int playGame(); // Main Program int main() { // Constants const int 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 (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) { } } ``````

Last edited on
 ``123456789101112131415161718`` ``````do { sum++; x = roll(); sumRoll = sumRoll + x; if(sumRoll > 100) { sumRoll = sumRoll - x; } cout << sum << " "; cout << endl; }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.
If you have a value, and you increase it by x, and then decrease it by x, you get the original value again.

Did you perhaps mean something like this:

 ``123456789101112131415161718`` ``````do { sum++; x = roll(); sumRoll = sumRoll + x; if(sumRoll > 100) { sumRoll = sumRoll - x; } cout << sum << " "; cout << endl; }while(sumRoll != 100);``````
Last edited on
Moschops, the code you posted unfortunately still continues the endless loop.
johnnydiamond08 wrote:
The endless loop is in the last function called playGame.
No, there is no endless loop.

By the way, you are using uninitialized data.
Topic archived. No new replies allowed.