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?
#include <iostream>
usingnamespace 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;
}
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)