continue?

Started by
8 comments, last by stormrunner 18 years, 8 months ago
Does c++ not recgonize the continue statment. Or does it get implemented in a differnt fashion then in c?
Do or do not there is no try -yoda
Advertisement
No, it is used the same way. Why do you ask?
#include <iostream>
#include <string>


using std::cin;
using std::cout;
using std::endl;
using std::string;


int main(void)
{
cout << "Difficulty levels\n\n";
cout << "1 - easy\n";
cout << "2 - Normal\n";
cout << "3 - hard\n\n";

int choice;
cout << "Choice: ";
cin >> choice;

switch(choice)
{
case 1: cout << "You picked Easy.\n";
break;

case 2: cout << "You picked normal.\n";
break;

case 3: cout << "You picked hard.\n";
break;

default: cout << "Invalid option, choose again.\n";
continue;

}
return 0;

I get an error on continues line. when i try to compile
Do or do not there is no try -yoda
continue isn't used like that in C, either. Just removing it should make the code work.
As far as I know, continue works fine in C++, and in the exact same way it does in C.

For example, this is a valid way to count from 1 to 10:

int k=0;while (1) {	k++;	cout << k << " ";	if (k<10) continue;	break;}


What compiler are you using?
dev c++

its probly something like a comman or oh wait, do you need to declare like a std::continue or some other declartion like that.

Im problying going to feel stupid when i figure this one out :P
Do or do not there is no try -yoda
Quote:Original post by Xloner12
dev c++

its probly something like a comman or oh wait, do you need to declare like a std::continue or some other declartion like that.

Im problying going to feel stupid when i figure this one out :P


continue is a keyword, not part of the standard library. Read my previous post for more information.
You use continue in loops, not switch statements.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
ahh, see I do feel stupid. I was using in a loop for that back in c. Then i try'ed it in a c++ statment now that im learning it. Bah. Thanks guys.
Do or do not there is no try -yoda
#include <iostream>#include <string>using std::cin;using std::cout;using std::endl;using std::string;int main(void){cout << "Difficulty levels\n\n";cout << "1 - easy\n";cout << "2 - Normal\n";cout << "3 - hard\n\n";int choice;cout << "Choice: ";cin >> choice;switch(choice){case 1: cout << "You picked Easy.\n";break;case 2: cout << "You picked normal.\n";break;case 3: cout << "You picked hard.\n";break;default: cout << "Invalid option, choose again.\n";continue;}return 0;

Your use of continue in this example isn't consistent with the examples that others have shown. The difference is that they use continue in a loop; your usage here is both illegal and impractical. Continue simply tells the loop to continue to the next iteration. Since you aren't using a loop, continue won't do anything. The following might be what you're looking for :
#include <iostream>#include <string>using std::cin;using std::cout;using std::endl;using std::string;int main(void){int choice;bool done = false;while( done != true){cout << "Difficulty levels\n\n";cout << "1 - easy\n";cout << "2 - Normal\n";cout << "3 - hard\n\n";cin >> choice;    switch(choice)  {  case 1: cout << "You picked Easy.\n";  done = true;  break;  case 2: cout << "You picked normal.\n";  done = true;  break;  case 3: cout << "You picked hard.\n";  done = true;  break;  default: cout << "Invalid option, choose again.\n";  done = false;  continue; // proper usage  }}return 0;}


<edit :: Far too slow [grin].
- stormrunner

This topic is closed to new replies.

Advertisement