Public Group

This topic is 3539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Back again, might be trying to tackle something to large for me. This time I'm looking at enumerators and conditional statements, specifically switch and if statements. I was able to get my program to work by doing something as simple as: int choice cin >> choice But that defeats the purpose of the enumerator I wanted to use in my switch statement, and therefore the purpose of my headache. I use the following code to try and get my switch statement to work:
Quote:
 #include using namespace std; int main() { enum difficulty {novice, easy, normal, hard, unbeatable}; cout << "Please enter a difficuly:\n\n 1 - Novice\n 2 - Easy\n 3 - Normal\n 4 - Hard\n 5 - Unbeatable" << endl; cin >> difficulty; switch (difficulty) { case novice: cout << "You picked Novice.\n"; break; case easy: cout << "You picked Easy.\n"; break; case normal: cout << "You picked Normal.\n"; break; case hard: cout << "You picked Hard.\n"; break; case unbeatable: cout << "You picked Unbeatable.\n"; break; default: cout << "Incorrect choice.\n"; } return 0; }
Now when it comes to the 'case x:' part of it, i've tried case 1, case value 1, case novice, case difficulty, case difficulty.novice, and almost anything else I can think of. Am I going about it in totally the wrong way? I can get it to work by changing it to: cin >> choice; switch (choice) {...} But whats the point of enumerators in this scenario if you have to leave them out? I've also tried: cin >> difficulty; choice=difficulty; switch (choice) {...} But to no avail. I'm at a loss. Do enumerators not work with switch at all? I tried it with if and the same thing. If they do work, how would I go about doing it?

##### Share on other sites
I can see the problems (code and logic), yet you do not tell us what the actual problem is. When posting a code related question as well as posting the code which is causing the error you also need to post the actual error.
I will refrain from posting the solution until you have posted the error and had a look to see if you can understand the problem.

##### Share on other sites
This doesn't work like this. You can't do

enum difficulty {...};
difficulty = easy;

The enum statement specifies a new type, not a new variable, so you should do this:

enum difficulty {...}
difficulty selectedDifficulty;

Now 'difficulty' has become a type like int or float. (Although internally it's just an int).

You have to put 'selectedDifficulty' inside you switch. Now you have the variable 'selectedDifficulty' which has type 'difficulty'.

Example:
int main(){enum difficulty {novice, easy, normal, hard, unbeatable};difficulty selectedDifficulty;cout << "Please enter a difficuly:\n\n 1 - Novice\n 2 - Easy\n 3 - Normal\n 4 - Hard\n 5 - Unbeatable" << endl;cin >> selectedDifficulty;switch (selectedDifficulty){case novice:cout << "You picked Novice.\n";break;case easy:cout << "You picked Easy.\n";break;case normal:cout << "You picked Normal.\n";break;case hard:cout << "You picked Hard.\n";break;case unbeatable:cout << "You picked Unbeatable.\n";break;default:cout << "Incorrect choice.\n";}return 0;}

##### Share on other sites
with enum difficulty {...} you declare a type. You must then create a variable of that type.

It should be something like the following:
enum difficulty {easy, hard};int main() { difficulty dif;  cin >> dif;  switch(dif) {  case easy:  break;  case hard:  break; }}

However you have a second problem, since 'cin' might not work for an enum. I'm not entirely sure how this works according to the C++ standard.. but I'm sure someone who knows will answer soon =)
You need to convert the input, which is an integer, to the enum.

It will probably work like this:
enum difficulty {easy=0, hard};int main() { difficulty dif; int i;  cin >> i; dif = (difficulty)i;  switch(dif) {  case easy:  break;  case hard:  break; }}

However you might want to check for valid input. What if the user inputs '6', which isn't even in your enum. You should probably do it something like this:
enum difficulty {unknown, easy, hard};int main() { difficulty dif = unknown; int i;  cin >> i;  switch(i) {  case 0:   dif = easy;  break;  case 1:   dif = hard;  break; }  switch(dif) {  case easy:  break;  case hard:  break;  default:   cout << " invalid input";  break; }}

##### Share on other sites
Quote:
 However you have a second problem, since 'cin' might not work for an enum. I'm not entirely sure how this works according to the C++ standard.. but I'm sure someone who knows will answer soon =)

I think it works because internally an enum is an int.

##### Share on other sites
Quote:
Original post by beun
Quote:
 However you have a second problem, since 'cin' might not work for an enum. I'm not entirely sure how this works according to the C++ standard.. but I'm sure someone who knows will answer soon =)

I think it works because internally an enum is an int.

It does not "work" and I would have explained this after the OP posted the error. :)

##### Share on other sites
Quote:
 I can see the problems (code and logic), yet you do not tell us what the actual problem is. When posting a code related question as well as posting the code which is causing the error you also need to post the actual error.I will refrain from posting the solution until you have posted the error and had a look to see if you can understand the problem.

D:\C++ Files\random test\main.cpp:11: error: expected primary-expression before ';' token
D:\C++ Files\random test\main.cpp:13: error: expected primary-expression before ')' token

That's the error I get. On line 11 and 13, I have what I thought was a correct statement, however it wasn't.

I did present my question. In different words it was, "how do i use enumerators with a switch/if statement."

I know it wasn't syntax because it work with a straight up int value, but not enum. I couldn't even begin to guess what error was since i was copying the syntax straight from a book, except I was attempting to use an enum vs int value. I didn't think that error would be of any use, however I will add it in the future. And just FYI, I spent a good 60+ minutes trying to figure out on my own before requesting help. Google included but all the articles I found were over my head in the context in which they were presented.

And I think I sound like an ass even though I'm not trying to.

EDIT: Alright now I am sufficiently confused. I changed cin. to cin.get(), compiled with no errors, however everything just returns to the default case. So apparently cin is not the correct method in which to retrieve user input data. Would anyone be willing to enlighten me as to what way would work?

##### Share on other sites
You can't input directly into a variable of an enumeration type. You can input into normal integers and then see if the value is one in the enumeration. How you do it is entirely up to you. I think a simple pattern is

enum difficulty { easy, normal, hard, max_difficulty };

and as long as you keep max_difficulty as the last you can always check:

int n;cin >> n;if (n >= 0 && n < max_difficulty) //OK

That said, it is possible to overload operator >> for enum types too if you really want to. E.g:

#include <iostream>using namespace std;enum difficulty {easy, medium, hard, max_difficulty};std::istream& operator>>(std::istream& is, difficulty& diff){    int n;    if (is >> n && (n >= 0 && n < max_difficulty)) {        //acceptable input, cast the result to enum type        diff = static_cast<difficulty>(n);    }    else {        //input doesn't match the criteria,        //set the failbit in stream (in addition to existing flags) to signal failure        is.clear(is.rdstate() | std::ios::failbit);    }    return is;}int main(){    difficulty dif;    if (!(cin >> dif)) {        cout << "bad input\n";    }    else {        switch(dif) {            case easy: cout << "easy\n"; break;            case medium: cout << "medium\n"; break;            case hard: cout << "hard\n"; break;            default:;        }    }}

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 16
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632660
• Total Posts
3007693
• ### Who's Online (See full list)

There are no registered users currently online

×