Jump to content
  • Advertisement
Sign in to follow this  
Sarxous

Enumeration Const Problems

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm once again trying another Exercise from the book "Beginning C++ Programming." The book asks me to rewrite a Menu Chooser program which was written to introduce switch statements, but this time I have to use enumeration. At this point I'm pretty conviced that it's impossible seeing as how you can't change const variables and I have to for the choosing of the options. Here is the code I have to rewrite:
Quote:
#include <iostream> using namespace std; int main() { cout << "Difficulty Levels\n\n"; cout << "1 - Easy\n"; cout << "2 - Normal\n"; cout << "3 - Hard\n"; int choice; cout << "Choice: "; cin >> choice; switch (choice) { case 1: cout << "You picked easy\n"; break; case 2: cout << "You picked normal\n"; break; case 3: cout << "You picked hard\n"; break; default: cout << "You made an illegal choice.\n"; } return 0; }
Here is my code:
Quote:
//Menu Chooser #include <iostream> #include <string> using namespace std; int main() { cout << "\t Menu Chooser Program.\n\n"; enum choice {Noice, Intermediate, Advanced}; cout << "1 - Novice\n"; cout << "2 - Average\n"; cout << "3 - Advanced\n\n"; cout << "Please enter your choice:"; cin >> choice; cin.get(); cin.get(); return 0; }

Share this post


Link to post
Share on other sites
Advertisement
enum choice {Noice, Intermediate, Advanced};

What this does is create a new type called choice.

So what you want to do, is move this definition outside the main function and then create a new variable of type choice:

enum choice {
Novice,
Intermediate,
Advanced
};

int main (int argc, char * argv[])
{
...
choice myChoice;
std::cout << "Choice: " << std::endl;
std::cin >> myChoice;
...
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Sarxous
The program is still getting errors on the "cin >> Mychoice;" line. Any idea why?
Since 'choice' is not a built-in type, the compiler can't find an appropriate overload for operator>>(). I don't know what the book intends to be the 'correct' solution, but one thing you could do would be to use an anonymous enum, and make myChoice a simple integer type.

Share this post


Link to post
Share on other sites
My new code:
Quote:
//Menu Chooser

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

int main()
{
cout << "\t Menu Chooser Program.\n\n";

enum choice {Noice, Intermediate, Advanced};
choice myChoice;

cout << "1 - Novice\n";
cout << "2 - Average\n";
cout << "3 - Advanced\n\n";

cout << "Please enter your choice:";
cin >> myChoice;

cin.get();
cin.get();
return 0;
}


The Errors:
Quote:
14 invalid conversion from `int' to `main()::choice'
21 no match for 'operator>>' in 'std::cin >> myChoice'

Share this post


Link to post
Share on other sites
Quote:
Original post by Sarxous
The Errors:
Quote:
14 invalid conversion from `int' to `main()::choice'
21 no match for 'operator>>' in 'std::cin >> myChoice'
Did you read my previous post?

Share this post


Link to post
Share on other sites
Quote:
Original post by Sarxous
It seems I missed it. Now if you'll excuse my noobism, might I ask just what is an anonymous enumerator is?


It's the same as a regular enum, but it doesn't have a name. The values become simple integer types.

enum {Noice, Intermediate, Advanced};

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!