Posted: Sat Mar 16, 2013 1:36 pm Post subject: Switch-Cases and enums
Hey, I learned how to do switch-cases and enums a week ago, but I didn't even practice any of it until now.
My compiler said at line 21: error: expected primary-expression before ';' token
and: error: expected primary-expression before ')' token.
I'm not sure how to fix this, so can someone help me please?
The code is:
because I just want to practice switch-cases and enums together like that.
Thanks.
Sponsor Sponsor
Insectoid
Posted: Sat Mar 16, 2013 1:49 pm Post subject: RE:Switch-Cases and enums
enums aren't strings. You can't just input them. In fact, what you're trying to do is pretty much impossible, as far as I know.
I know, this makes them seem pretty useless. They aren't anything like hashmaps in other languages. They actually are pretty useless. I've never, ever used one before and I've never had a moment where I thought it might come in handy.
Zren
Posted: Sat Mar 16, 2013 2:33 pm Post subject: RE:Switch-Cases and enums
cin >> Option;
It's expecting Option.Op_1 or something I think. Or it's foobaring because it needs an assignable variable. Either way, you need to use cin to assign the input to a variable. Option is a type. You need to declare a variable as Option option; and assign to that. Same logic for the second error.
Not sure if cin will convert user input to int type (which is essentially what an enum is), so you might need to manually parse the user input.
Enums are handy for compile time states, though I haven't used them in C++.
You (probably) only need to set the first option if you want to start from 1 by the way.
Eg:
c++:
// If you leave out the assignment, it will be the last value +1. enum Gender { MALE = 1, FEMALE }; // Gender.FEMALE should be equivalent to 2.
Cancer Sol
Posted: Sat Mar 16, 2013 3:23 pm Post subject: Re: RE:Switch-Cases and enums
Insectoid @ 3/16/2013, 1:49 pm wrote:
enums aren't strings. You can't just input them. In fact, what you're trying to do is pretty much impossible, as far as I know.
I know, this makes them seem pretty useless. They aren't anything like hashmaps in other languages. They actually are pretty useless. I've never, ever used one before and I've never had a moment where I thought it might come in handy.
I know enums are integers. I assigned each variable there a value as an integer though, and, for example, if the user inputs 1, then the program will output "One".
Maybe I shouldn't use enums then :/
Just wondering, how many other people here also do not use enums? Are they that useless?
@Zren What does foobaring mean? I didn't read anything about foobaring so I don't know what it is :/
Thanks for the help though, guys!
Edit: @Zren Why is it Option option though?
Insectoid
Posted: Sat Mar 16, 2013 3:36 pm Post subject: RE:Switch-Cases and enums
I also realized you forgot the break statements when writing this example.
Insectoid
Posted: Sat Mar 16, 2013 3:51 pm Post subject: RE:Switch-Cases and enums
Quote:
Edit: @Zren Why is it Option option though?
Option isn't a variable. It's a type. Just like an int is a type. If you have int foo, then int is the type and foo is the variable name. With Option option, Option is the type and option is the variable name. You could also do Option foo if you wanted.
Sponsor Sponsor
Cancer Sol
Posted: Sat Mar 16, 2013 4:41 pm Post subject: Re: RE:Switch-Cases and enums
Insectoid @ 3/16/2013, 3:51 pm wrote:
Quote:
Edit: @Zren Why is it Option option though?
Option isn't a variable. It's a type. Just like an int is a type. If you have int foo, then int is the type and foo is the variable name. With Option option, Option is the type and option is the variable name. You could also do Option foo if you wanted.
I didn't know there was a type called Option... or was that because of the enum?
Insectoid
Posted: Sat Mar 16, 2013 5:03 pm Post subject: RE:Switch-Cases and enums
When you create a enum, you are defining a type. In Zren's example above, Gender is a type.
Cancer Sol
Posted: Sat Mar 16, 2013 6:58 pm Post subject: Re: RE:Switch-Cases and enums
I also realized you forgot the break statements when writing this example.
Oh.. sorry, I didn't see your post. I just Lol'd at case MALE xD
I'm still kinda confused with enums, I'll just reread the e-book I have over and over again then.
About the break statements... I always forget to put them
Btw, if I wanted to end the loop inside a switch-case, how do I do it? Since a break statement would only end the switch-case, is there another statements I'm supposed to use?
Insectoid
Posted: Sat Mar 16, 2013 7:05 pm Post subject: RE:Switch-Cases and enums
If it's a while loop, set the exit case to true. If it's a for loop, put the incrementor out of bounds.
Cancer Sol
Posted: Sat Mar 16, 2013 7:27 pm Post subject: Re: RE:Switch-Cases and enums
Insectoid @ 3/16/2013, 7:05 pm wrote:
If it's a while loop, set the exit case to true. If it's a for loop, put the incrementor out of bounds.
Can you give an example please?
Insectoid
Posted: Sat Mar 16, 2013 7:31 pm Post subject: RE:Switch-Cases and enums
[code]
for (bool exit = false; !exit;){
switch (something){
case 1:
exit = true;
break;
}
}
Cancer Sol
Posted: Sun Mar 17, 2013 8:15 pm Post subject: Re: RE:Switch-Cases and enums
Insectoid @ 3/16/2013, 7:31 pm wrote:
[code]
for (bool exit = false; !exit;){
switch (something){
case 1:
exit = true;
break;
}
}
Isn't an exit statement supposed to close the program? Or is it different because of the switch-case? What does it actually do in a switch-case though?