continue statement not within loop?

I've been learning C++ for about 2 weeks so I'm pretty new to it. I've started on switch statements in a chapter of a book about looping. Apparently the continue statement in this small program I made isn't in the loop (even though it obviously is). What's going on with it? :(... Also unless I put a semicolon by <when (finished = false)> the loop doesn't even excecute... What have I done wrong?

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
#include <iostream>

using namespace std;

int main()
{
    int choice, end;
    bool finished;
    finished = false;


    while (finished = false)
    {
        cout << "Enter a number between 1 to 5" << "\n";
        cin >> choice;

        switch(choice)
        {
            case 0:
                cout << "Too small mate";
                break;
            case 5:
                cout << "ARRRR!!! ";
                break;
            case 4:
                cout << "OOOAAARRR!!! ";
                break;
            case 3:
                cout << "I could really do with a sandwich ";
                break;
            case 2:
                cout << "Yum... ";
                break;
            case 1:
                cout << "This is the best number because I said so. ";
                break;
            default:
                cout << "Number way too high mate";
                break;
        }
        cout << "\n";
        cout << "Make another awesome sentence appear? 1 for Yes, 0 for no." << "\n";
        cin >> end;

        switch(end)
        {
            case 1:
            //
            case 0:
                finished = true;
                break;
            default:
                cout << "inputted wrongly... Ending program regardless of what you want.";
                break;
        }
        if (finished = false)
        {
            continue;
        }

        cout << "done";
    }
    return 0;
}


Last edited on
while (finished = false)

Should be:

while ( finished == false )

Your using the assignment operator instead of equal to.

Same issue here:
if (finished = false)

Also, you need a break statement in case 1 or it will move on and execute:

1
2
finished = true;
break;


Ending the loop.
Last edited on
You are assigning the value of false to the variable finished. Therefore the loop can never execute. In this case the result of the assignment is false.
while (finished = false)

Try this.
while (!finished)

or
while (false == finished)

I recommend that you develop the habit of writing your conditional checks like above. If not a boolean put the constant on the left side of the '==' so that the compile will catch accidental uses of '='. For instance the following code will not compile.
while(false = finished)
Last edited on
1
2
3
4
if (finished = false)
        {
            continue;
        }


That block of code is totally unnecessary. Delete it and move line 61 outside of the while loop.

With all the information on here I've fixed it and commented where I went wrong :), thanks a bunch guys.

And finally, @ return 0 -- Does "=" mean assign and "==" mean equal to?

It's so easy to miss things in this language... VB really is lenient XD

Thanks again anyway guys :D
And finally, @ return 0 -- Does "=" mean assign and "==" mean equal to?


Correct.
Topic archived. No new replies allowed.