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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214


//----------------
// Include Section
//----------------
#include <iostream>
#include <string>
#include <iomanip>
#include <math.h>
#include <stdlib.h> //contains description of srand function
#include <time.h>
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 <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)

    {
        
        
        
        
    }


}








Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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.