Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Switch-Cases and enums
Index -> Programming, C++ -> C++ Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cancer Sol




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: 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




PostPosted: 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




PostPosted: 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?
Insectoid




PostPosted: 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.
Cancer Sol




PostPosted: 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?
Zren




PostPosted: 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.
Insectoid




PostPosted: 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
Sponsor
sponsor
Cancer Sol




PostPosted: 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




PostPosted: 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




PostPosted: 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?
Insectoid




PostPosted: 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




PostPosted: 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
Insectoid




PostPosted: 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




PostPosted: 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?
Display posts from previous:   
   Index -> Programming, C++ -> C++ Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: