continue?
Does c++ not recgonize the continue statment. Or does it get implemented in a differnt fashion then in c?
#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
#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
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:
What compiler are you using?
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
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
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.
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.
#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].
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement