Computer Science Canada

Switch-Cases and enums

Author:  Cancer Sol [ 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? Smile
The code is:
c++:

#include <iostream>
#include <string>
using namespace std;

void skipline () //Function Skips line
{
    cout << endl;
}

int main ()
{
    enum Option {Op_1=1, Op_2=2, Op_3=3, Op_4=4, Op_5=5}; // Declares option, Op_1 has the value of 1, and is used for the user input for "Option"
    cout << "Choose an option from the list:" << endl
         << "Option 1 outputs one." << endl
         << "Option 2 outputs two." << endl
         << "Option 3 outputs three." << endl
         << "Option 4 outputs four." << endl
         << "Option 5 outputs five." << endl;
    skipline (); // Skips a line
    cout << "Input the option here: ";
    cin >> Option;
    skipline ();
    switch (Option)
    {
        case Op_1: cout << "One";
        case Op_2: cout << "Two";
        case Op_3: cout << "Three";
        case Op_4: cout << "Four";
        case Op_5: cout << "Five";
        default: cout << "Invalid input.";
    }
}

Please don't say "You can just assign Option as an integer and have the switch-case like this:"
c++:

switch (option)
{
    case 1: cout << "One";
    case 2: cout << "Two";
    //Etc.
}

because I just want to practice switch-cases and enums together like that.

Thanks.

Author:  Insectoid [ 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.

Author:  Zren [ 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.

Author:  Cancer Sol [ 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? Razz

@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?

Author:  Insectoid [ Sat Mar 16, 2013 3:36 pm ]
Post subject:  RE:Switch-Cases and enums

Fubar.

In short code demonstrations, foo and bar are common variable names, so 'fubar' was adopted and adapted for our own use.

Author:  Cancer Sol [ Sat Mar 16, 2013 3:42 pm ]
Post subject:  Re: RE:Switch-Cases and enums

Insectoid @ 3/16/2013, 3:36 pm wrote:
Fubar.

In short code demonstrations, foo and bar are common variable names, so 'fubar' was adopted and adapted for our own use.

Lol? Fubar is supposed to be some kinda military slang word?

Author:  Zren [ Sat Mar 16, 2013 3:49 pm ]
Post subject:  Re: RE:Switch-Cases and enums

Insectoid @ Sat Mar 16, 2013 3:36 pm wrote:
Fubar.

In short code demonstrations, foo and bar are common variable names, so 'fubar' was adopted and adapted for our own use.


Thanks for the save. Totally swapped the two words.

Cancer Sol wrote:

Edit: @Zren Why is it Option option though?


Because you're declaring a variable named option of type Option.

c++:

enum Gender { UNKNOWN, MALE, FEMALE };
Gender zrensGender = Gender.UNKNOWN;

switch (zrensGender) {
  case MALE:
    cout << "Balls";
    break;
  default:
    cout << "Other";
    break;
}


I also realized you forgot the break statements when writing this example.

Author:  Insectoid [ 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.

Author:  Cancer Sol [ 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?

Author:  Insectoid [ 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.

Author:  Cancer Sol [ Sat Mar 16, 2013 6:58 pm ]
Post subject:  Re: RE:Switch-Cases and enums

Zren @ 3/16/2013, 3:49 pm wrote:
Insectoid @ Sat Mar 16, 2013 3:36 pm wrote:
Fubar.

In short code demonstrations, foo and bar are common variable names, so 'fubar' was adopted and adapted for our own use.


Thanks for the save. Totally swapped the two words.

Cancer Sol wrote:

Edit: @Zren Why is it Option option though?


Because you're declaring a variable named option of type Option.

c++:

enum Gender { UNKNOWN, MALE, FEMALE };
Gender zrensGender = Gender.UNKNOWN;

switch (zrensGender) {
  case MALE:
    cout << "Balls";
    break;
  default:
    cout << "Other";
    break;
}


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 Sad
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?

Author:  Insectoid [ 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.

Author:  Cancer Sol [ 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? Smile

Author:  Insectoid [ 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;
}
}

Author:  Cancer Sol [ 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?

Author:  DemonWasp [ Sun Mar 17, 2013 9:26 pm ]
Post subject:  RE:Switch-Cases and enums

exit isn't a statement in C/C++. In the code Insectoid posted, it's a boolean variable initialized in the initializer part of the for loop.

Note that 'exit' can also refer to the exit() method (available on Unix/Linux systems, I don't know about Windows). However, method invocations always have round brackets attached in C/C++, which means that referring to 'exit' without '()' must be referring to a variable.

Author:  Cancer Sol [ Mon Mar 18, 2013 12:05 pm ]
Post subject:  Re: Switch-Cases and enums

Oh I see... what does ! do that's beside exit in the for loop?
code:

for (bool exit = false; !exit;){
switch (something){
case 1:
exit = true;
break;
}
}

Author:  DemonWasp [ Mon Mar 18, 2013 12:28 pm ]
Post subject:  RE:Switch-Cases and enums

! is the unary not operator. With booleans, it flips the value: true becomes false, false becomes true.

If you're going to learn C++, you really need to read a book that will take you through all of this.

Author:  Cancer Sol [ Wed Mar 20, 2013 6:57 pm ]
Post subject:  Re: RE:Switch-Cases and enums

DemonWasp @ 3/18/2013, 12:28 pm wrote:
! is the unary not operator. With booleans, it flips the value: true becomes false, false becomes true.

If you're going to learn C++, you really need to read a book that will take you through all of this.


Well.. I started studying them on the internet Razz
I really gotta learn all those lol, just some new stuff I learned can shorten my code a lot, and make it easier to read.


: