Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Hello every1
Can anyone explain to me why doesn't this program work?
#include <iostream>
using namespace std;
int main(){
enum enumDif{EASY, MEDIUM, HARD};
enumDif difficulty;
cout << "Difficluty Levels:\n";
cout << "1 - Easy\n";
cout << "2 - Medium\n";
cout << "3 - Hard\n\n";
cin >> difficulty;
switch(difficulty){
case 1: cout << "You picked Easy\n";
break;
case 2: cout << "You picked Medium\n";
break;
case 3: cout << "You picked Hard\n";
break;
}
system("Pause");
}I must confess, i never understood the enum type of variable very well, probably that is were the program's error lies...if it is please explain me ^^
Best Regards!
AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;

I do not think it should compile, much less work.
This line:
cin >> difficulty;
should generate an error message something like:
error: no match for ‘operator>>’ in ‘std::cin >> difficulty’
Here is the quote from Stroustrup:
"An enumeration is a user-defined type, so users can define their own operations, such as ++ and << for an enumeration."
Presumably operator>> as well.
Guy

hum...oookay...sorry, i still dont get it can you give me a practical example?
AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;

The only error I got was:
Function 'system' should have a prototype in function main()
After I deleted that line, it compiled and ran.
=====================================
If at first you don't succeed, you're about average.M2

As with anything in C++, it depends on what your complier will let you get away with. To answer your question, a version which should work with most compliers is:
#include <iostream>
using namespace std;int main(){
enum enumDif{EASY = 1, MEDIUM, HARD};
int difficulty;
cout << "Difficluty Levels:\n";
cout << "1 - Easy\n";
cout << "2 - Medium\n";
cout << "3 - Hard\n\n";cin >> difficulty;
switch(static_cast<enumDif>(difficulty)) {
case EASY: cout << "You picked Easy\n";
break;
case MEDIUM: cout << "You picked Medium\n";
break;
case HARD: cout << "You picked Hard\n";
break;
}
return 0;
}You see? Basically, enumerations let you assign words to numbers. They are technically a different type, so we had to static_cast<> it in the switch.
EDIT: Also valid: if (difficulty == EASY)

Let me try to understand and correct me if im wrong please:
you made the unsigned int EASY equal to 1 so that MEDIUM is 2 and HARD is 3;
you made "difficulty" an int so that it can store 1, 2 or 3 typed by the user;
and finnaly you used static_cast<> to convert the enumDif type of variable to an int and insted of having case 1, 2 and 3 you made the cases EASY MEDIUM and HARD beacause they worth 1, 2 and 3 ?
Now, i just didnt undestand the static cast part, i think i didnt got there on my book(Begining C++ game programing)...
Best Regards
PS: My compiler's Dev-C++
AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;

you made the unsigned int EASY equal to 1 so that MEDIUM is 2 and HARD is 3;
Correct.you made "difficulty" an int so that it can store 1, 2 or 3 typed by the user;
Correct.and finnaly you used static_cast<> to convert the enumDif type of variable to an int
The other way around; I casted the int as an enumDif.and insted of having case 1, 2 and 3 you made the cases EASY MEDIUM and HARD beacause they worth 1, 2 and 3 ?
Correct.

Great i got it now!
Thanks!!AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |