Need Help looping

How would I condense this into loops so I don't have to repeat all this code? And no I don't want to call a function. Just loop :) Thanks. I am just not sure how to without having the same result. I have been trying for hours.
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
//earlier in code user chooses a number 1-8 and thats where the number starts and adds or subtracts 0.5 depending on the below randomizer


	srand(time(0)); //randomize either a zero or a one
        r = rand() % 2;


if (r == 0) // if a zero add 0.5, if a one, subtract 0.5 and repeat 12 times
{
	selection = selection + 0.5;
	cout << selection << endl;
}
else
{
	selection = selection - 0.5; 
	cout << selection << endl;
}

  r = rand() % 2;
if (r == 0)
{
	selection = selection + 0.5;
	cout << selection << endl;
}
else
{
	selection = selection - 0.5; 
	cout << selection << endl;
}

  r = rand() % 2;
if (r == 0)
{
	selection = selection + 0.5;
	cout << selection << endl;
}
else
{
	selection = selection - 0.5; 
	cout << selection << endl;
}

  r = rand() % 2;
if (r == 0)
{
	selection = selection + 0.5;
	cout << selection << endl;
}
else
{
	selection = selection - 0.5; 
	cout << selection << endl;
}

  r = rand() % 2;
if (r == 0)
{
	selection = selection + 0.5;
	cout << selection << endl;
}
else
{
	selection = selection - 0.5; 
	cout << selection << endl;
}

  r = rand() % 2;
if (r == 0)
{
	selection = selection + 0.5;
	cout << selection << endl;
}
else
{
	selection = selection - 0.5; 
	cout << selection << endl;
}

  r = rand() % 2;
if (r == 0)
{
	selection = selection + 0.5;
	cout << selection << endl;
}
else
{
	selection = selection - 0.5; 
	cout << selection << endl;
}

  r = rand() % 2;
if (r == 0)
{
	selection = selection + 0.5;
	cout << selection << endl;
}
else
{
	selection = selection - 0.5; 
	cout << selection << endl;
}

  r = rand() % 2;
if (r == 0)
{
	selection = selection + 0.5;
	cout << selection << endl;
}
else
{
	selection = selection - 0.5; 
	cout << selection << endl;
}

  r = rand() % 2;
if (r == 0)
{
	selection = selection + 0.5;
	cout << selection << endl;
}
else
{
	selection = selection - 0.5; 
	cout << selection << endl;
}

  r = rand() % 2;
if (r == 0)
{
	selection = selection + 0.5;
	cout << selection << endl;
}
else
{
	selection = selection - 0.5; 
	cout << selection << endl;
}

  r = rand() % 2;

if (r == 0)
{
	selection = selection + 0.5;
	cout << selection + where_drop << endl;
}
else
{
	selection = selection - 0.5; 
	cout << selection + where_drop << endl;
}

 // below is the results. if you end at zero after 12 randoms, then you get 100 bucks and so on

if (where_drop = 0){
	user_winnings = 100;
}
if (where_drop = 1){
	user_winnings = 500;
}
if (where_drop = 2){
	user_winnings = 1000;
}
if (where_drop = 3){
	user_winnings = 0;
}
if (where_drop = 4){
	user_winnings = 10000;
}
if (where_drop = 5){
	user_winnings = 0;
}
if (where_drop = 6){
	user_winnings = 1000;
}
if (where_drop = 7){
	user_winnings = 500;
}
if (where_drop = 8){
	user_winnings = 100;
}

	cout<<"You've won $" << user_winnings << "!" << endl;


Loops are all about generalizing a pattern.
Look at what each if/else is doing. Each of them is doing: r = rand() % 2; and then checking results. You also know that you want these checked 12 times. Since the loop is limited by a counting value this sounds like a good candidate for a 'for' loop.

Just based on that you could have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
srand(time(0));  //seed the random number
for(int i=1; i<=12; i++) //12 iterations starting from 1
{
  r = rand() % 2;
  if (r == 0)
  {
	selection = selection + 0.5;
	cout << selection << endl;
  } 
  else
  {
	selection = selection - 0.5; 
 	cout << selection << endl;
  }
}



As it turns out the top loop can be simplified even more by adjusting the output from the random number to fit ur needs:
1
2
3
4
5
6
7
8
srand(time(0));  //seed the random number
//r = rand() % 2;  //randomize either a zero or a one //uneeded

for(int i=0; i<12; i++) //12 iterations starting from 0
{
    selection += (rand() % 2) - 0.5;
    cout << selection << endl;
}



Is there a pattern in lines 154 to 180 ?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (int i = 0; i < 12; i++)
{
   int r = rand() % 2;
   if (r == 0)
   {
	selection = selection + 0.5;
	cout << selection << endl;
   } 
   else
   {
	selection = selection - 0.5; 
	cout << selection << endl;
   }
}


By the way, your if statements at the end won't do what you expect them to. Each clause is actually an assigment statement (=) rather than an "is equal" statement (==). Each assignement that is not 0 will evaluate to true, so userWinnings will be set to 500, 1000, 0, 10000, 0, 1000, 500 and 100 each time through.

thanks a ton for the help
actually how would I set the parameters so that "selection" couldn't go below zero or higher than 10?
ifs and else ifs
inside here? or outside of it
1
2
3
4
5
6
7
8
9
10
 if (randomizer == 0)
  {
    selection = selection + 0.5;
    cout << selection << endl;
  } 
  else
  {
    selection = selection - 0.5; 
     cout << selection << endl;
  }
Is this the 'selection' from the user ? or the adjusted selection from the +- 0.5 loop ? What do you want to happen if it does hit 10 or 0? Stop? Only count in the opposite direction ?
if the adjusted selection ever hits 0 or 10 it goes back to 0.5 or 9.5 respectively so it never goes under/over. just keep the adjusted selections always in between 0-10
1
2
3
4
5
6
7
8
9
10
11
12
13
srand(time(0));  //seed the random number
//r = rand() % 2;  //randomize either a zero or a one //uneeded

for(int i=0; i<12; i++) //12 iterations starting from 0
{
    selection += (rand() % 2) - 0.5;
    if(selection >= 10)
         selection -= 0.5;
    if(selection <= 0)
         selection += 0.5;
        
    cout << selection << endl;
}
Last edited on
i did the above but it didn't work for me. it just got messed up from the original I posted above.
Topic archived. No new replies allowed.