Computing.Net > Forums > Programming > noob program doubt

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

noob program doubt

Reply to Message Icon

Name: Deimos
Date: November 3, 2007 at 09:21:41 Pacific
OS: Windows Xp professional S
CPU/Ram: CPU:2.6x2Ghz Ram:2048mbs
Product: Deimos's nr1
Comment:

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;



Sponsored Link
Ads by Google

Response Number 1
Name: Guy
Date: November 3, 2007 at 12:46:56 Pacific
Reply:

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


0

Response Number 2
Name: Deimos
Date: November 3, 2007 at 12:49:37 Pacific
Reply:

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;


0

Response Number 3
Name: Mechanix2Go
Date: November 3, 2007 at 15:47:25 Pacific
Reply:

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



0

Response Number 4
Name: Razor2.3
Date: November 4, 2007 at 02:45:58 Pacific
Reply:

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)


0

Response Number 5
Name: Deimos
Date: November 4, 2007 at 06:11:30 Pacific
Reply:

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;


0

Related Posts

See More



Response Number 6
Name: Razor2.3
Date: November 4, 2007 at 06:25:46 Pacific
Reply:

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.


0

Response Number 7
Name: Deimos
Date: November 4, 2007 at 07:20:40 Pacific
Reply:

Great i got it now!
Thanks!!

AMD ATHLON X2 5200 2.6ghz;
ASUS M2N-E SLI;
2GB DDR800 KINGSTON;
ASUS GF8600GTS;
Seagate 7200rpm 320GB;


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: noob program doubt

Noob Programming... www.computing.net/answers/programming/noob-programming/3128.html

doubt in C++ programming www.computing.net/answers/programming/doubt-in-c-programming/13142.html

Getting into programing www.computing.net/answers/programming/getting-into-programing/8514.html