1

So, I am a complete beginner with the c++ language, and I am working on an exercise in the book "Beginning C++ Through Game Programming", specifically the first exercise on chapter 3. It wants me to create a "Difficulty Choosing Program" using std::cout and the switch statement, however I found an issue when trying to use std::cin to input a variable (myDifficulty) of the enumeration type difficulties, where I get the error above under my >>. Here is my code:

#include <iostream>

int main()
{
enum difficulties {EASY = 1, NORMAL, HARD, EXTREME};
difficulties myDifficulty;

std::cout << "Easy\nNormal\nHard\nExtreme\n";
std::cin >> myDifficulty;

switch (myDifficulty)
{
    case 1:
        std::cout << "You have chosen Easy as your difficulty." << std::endl;
        break;
    case 2:
        std::cout << "You have chosen Normal as your difficulty." << std::endl;
        break;
    case 3:
        std::cout << "You have chosen Hard as your difficulty" << std::endl;
        break;
    case 4:
        std::cout << "You have chosen Extreme as your difficulty. Good luck!" << std::endl;
        break;
}
system("pause");
return 0;

}

If anyone can help, thank you. Also, if there is anything wrong with my code (how it looks etc.) feedback would be appreciated greatly!

P.S This is my first question. :)

quantdev
  • 23,517
  • 5
  • 55
  • 88
MitarashiFox
  • 13
  • 1
  • 5
  • `enum` type doesn't have a `>>` operator. Take a look [here](http://stackoverflow.com/questions/10371681/enum-type-can-not-accept-cin-command). – ctzdev Jun 30 '14 at 20:51
  • Also read: [Operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading) – Deduplicator Jun 30 '14 at 20:55
  • You need to implement a global function `istream& operator>>(istream& is, difficulties& dif)`. – barak manos Jun 30 '14 at 20:55
  • 1
    I would use `case EASY:`, `case NORMAL:`, etc. rather than `case 1:`, `case 2:` etc. – aschepler Jun 30 '14 at 21:09
  • 1
    When a user sees "Easy Normal Hard Extreme", he will not know what to do. You should prompt "Please enter difficulty: 1=Easy, 2=Normal ...". Or something like "[E]asy [N]ormal [H]ard [E]xtreme" to hint that your program expects a letter. Or a question: "Which difficulty do you want? (Easy, Normal, etc)" – anatolyg Jun 30 '14 at 21:22
  • Thanks anatolyg, I just noticed that right before you commented. A rookie is bound to make mistakes I suppose. xD – MitarashiFox Jun 30 '14 at 21:24

1 Answers1

3

There is no defined operator>> for enum class in C++ by default, you need to create your own operator:

std::istream& operator>> (std::istream& in, difficulties& diff) ;
Captain Giraffe
  • 14,407
  • 6
  • 39
  • 67
Holt
  • 36,600
  • 7
  • 92
  • 139
  • ... and then define it. – Lightness Races in Orbit Jun 30 '14 at 21:15
  • Thank you Captain Giraffe and Holt, but there is one little issue. Under `operator` I am getting an error that states "function "operator>>", declared using a local type, must be defined in this translation unit." – MitarashiFox Jun 30 '14 at 21:21
  • Thank you to everyone who answered in such short time! I found out that I wasn't supposed to use an enumeration type int, I was supposed to just use an int. Whoopsy daisy. However, I finally got the program to work, so once again, thanks! – MitarashiFox Jun 30 '14 at 21:48