Using a loop

I just started programming a little under a month ago as a college course, and I am working on a "Wheel of Fortune" program. So far I have the switch and cases in place along with the basic shell, however I realized I need a loop so users would be able to select multiple selections as a very basic representation of the game show. We have used loops once or twice but since I am so very new to all of this I honestly don't remember how to use a loop. I would really appreciate any help.
You know
1
2
3
4
if (condition)
{
    do something
}


Now learn:
1
2
3
4
while (condition)
{
    do something
}


This just means that while the condition is true, do the code in the brackets. That's all there is to it.
So if I use the (if,do) statement, that should allow me to set a certain number of spins before the program would end. Correct?
The general opinion is that if you have a set number of times you want to loop you normally use a for loop. See: http://cplusplus.com/doc/tutorial/control/#for for more information about for loops (and other control structures)
In the if example above, the code inside the braces will execute once if the condition is true. You could do this multiple times if you included it inside of a for loop or while loop.

In the while example above, the code inside the braces will execute (potentially) multiple times as long as the condition at the is true for each iteration of the loop. Note, when using a while loop, be sure to have something that alters the condition. Otherwise, you'll find yourself caught in an infinite loop.

I'm unclear on your particular needs, but you could have something along the lines of this.

1
2
3
4
5
6
7
int spins = 3;

while (spins)
{
   cout << "Wheel is spinning" << endl;
   spins--;
}


In my example, this will print the "Wheel is spinning" message three times.

You could even shorten this to:

1
2
3
4
5
6
int spins = 3;

while (spins--)
{
   cout << "Wheel is spinning" << endl;
}


EDIT: Here's a for loop example that'll do the same thing.

1
2
3
4
5
6
const int NUM_SPINS = 3;

for (int i = 0; i < NUM_SPINS; i++)
{
   cout << "Wheel is spinning" << endl;
}


Hope this helps.
Last edited on
This is the code I am working on, if it helps. When it is finished I want to be able to allow users to spin the wheel 5 times and see how much they have won if they don't hit bankrupt.

@ iHutch105: Would I insert the while loop before or after the cases? I struggle the most where and when to put everything ha ha. Thank you all for your help so far I really appreciate it.

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
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

int main()
{
    int guess;
    cout <<"Please spin the wheel!" << endl;
    cin >> guess;
    
    srand(time(0));
    int wheelspin = (rand() % 16) + 1;
    cout << wheelspin << endl;
    switch(wheelspin)
    {
         case 1: 
              cout << "$100" <<endl;
              break;
              
         case 2: 
              cout << "$200" <<endl;
              break;
              
         case 3: 
              cout << "$300" << endl;
              break;
              
         case 4: 
              cout << "$400" << endl;
              break;
              
         case 5: 
              cout << "$500" << endl;
              break;
              
         case 6: 
              cout << "$1,000" << endl;
              break;
              
         case 7: 
              cout << "Bankrupt!" << endl;
              break;
              
         case 8: 
              cout << "$2,000" << endl;
              break;
              
         case 9: 
              cout << "$2,500" << endl;
              break;
              
         case 10: 
              cout << "$5,000" << endl;
              break;
              
         case 11: 
              cout << "Lose a Turn!" << endl;
              break;
                            
         case 12: 
              cout << "$10,000" << endl;
              break;
               
         case 13: 
              cout << "$20,000" << endl;
              break;
              
         case 14: 
              cout << "$25,000" << endl;
              break;
              
         case 15: 
              cout << "$50,000" << endl;
              break;
              
         default: 
                  cout << "Invalid input." << endl;
    }
    system("pause");
    return 0;
}
Last edited on
Ok, so you'll want to encompass a lot of that inside of a loop. You can choose your poison from the examples above. For example,you could set a number of spins, use it as a while condition and at the end of the while loop, take a spin off that amount.

A couple of notes though:

1) You'll probably want to refactor this a little bit. Maybe declare your variables before the loop so that went you enter a loop you're not redeclaring them. It won't make much difference, but it's good coding practice.

2) Why don't you have a counter that sums up the money won so far? So in each case statement you add the money to the running total. That way, if they hit bankrupt, you can set that value back to zero. Then, if they have any spins left, the money will start counting up from zero.

Once all the spins are used (i.e. outside of your loop) you could then output (using a total variable as in point 2 above) the value they have at the end of the game.
I was going to do the counter last since, being the green horn that I am, I don' really know how to do that.

Also I am thinking of starting the loop after the first bracket.
Last edited on
Sure, it could go in the first bracket, but you'd be redeclaring a few variables there too. As I mentioned earlier, it wouldn't do a great deal of harm, but it's better practice to not redeclare.

If you want to deal with the counter later, that's fine and it's probably not as hard as you think. I can help you with that, no problem. But let's take things a step at a time and get the loop sorted first.

I find it helpful to write a functionality list, detail what I want my program to do. It helps me order things, gives context and makes it easier to code. Here's what I'd do in your instance:

1
2
3
4
5
6
7
8
9
10
// Declare variables for input, loop condition and wheel position
// Set the loop condition variable
// Seed the random number from time
// -- Start a loop (up to you which kind, personal preference, really)
// -- Ask the user to spin the wheel (essentially, press any key then enter in your case)
// -- Generate a random number
// -- Execute switch statement (which currently will output cout statements)
// -- If it's a while loop, change the condition (important!!)
// -- Exit loop
// Output some kind of end message 


The 'change the condition' step is important, unless you're using a for loop (where the condition is changed in the actual loop call at the top) or you're altering the condition when calling the while loop (see my second example of a while loop above). If you're using a regular while, like my first while example, and you don't add a (suitable) change to the condition, you're going to find yourself stuck in an infinite loop.

So in the functionality list there, your loop condition variable dictating how many times you want the loop to run:

 
int iterations = 5;


And that would be used for counting in your loop:

1
2
3
4
5
while (iterations) // Note: This is the same as saying while iterations != 0
{
   // Do stuff
   iterations--;
}


Last edited on
Topic archived. No new replies allowed.