Enumerator Help! [SOLVED]

Started by
5 comments, last by yaustar 17 years, 11 months ago
Hello. I was trying to complete an exercise at the end of chapter 2 of my c++ book, and i came across a problem i cannot figure out. The error says, "no math for 'operator >>' in 'std::cin >> myDiff'". I have no clue as to how to fix it, so maybe someone can help me out. Thanks for any help. Heres my code:

#include <iostream>
#include <windows.h>
 
int main()
{
    enum difficulty {EASY = 1, NORMAL, HARD};
    difficulty myDiff;
    
    std::cout << "Welcome!\n\n"
              << "1 : Easy\n"
              << "2 : Normal\n"
              << "3 : Hard\n\n"
              << "Selection - ";
    std::cin >> myDiff;  
    
    if(myDiff == 1)
    {
        std::cout << "\n\nDifficulty set to Easy\n\n";
    }
    if(myDiff == 2)
    {
        std::cout << "\n\nDifficulty set to Normal\n\n";
    }
    if(myDiff == 3)
    {
        std::cout << "\n\nDifficulty set to Hard\n\n";
    }
    
    //Waits till user terminates the program.
    system("PAUSE");
    return 0;
}


[Edited by - NUCLEAR RABBIT on April 29, 2006 5:41:59 PM]
Advertisement
Quote:Original post by NUCLEAR RABBIT
Hello. I was trying to complete an exercise at the end of chapter 2 of my c++ book, and i came across a problem i cannot figure out. The error says, "no math for 'operator >>' in 'std::cin >> myDiff'". I have no clue as to how to fix it, so maybe someone can help me out. Thanks for any help.

Heres my code:

*** Source Snippet Removed ***


Well I don't know how it goes in C++, but in my prefered language(C#), this line of code:

enum difficulty {EASY = 1, NORMAL, HARD};

Would make EASY, NORMAL, and HARD all equal to 1. Also, your enum can't(I think) be used as a variable like a struct or class. If it were me coding, this is what I would do:

#include <iostream>#include <windows.h> int main(){    enum difficulty {EASY = 1, NORMAL, HARD};    int choice;        std::cout << "Welcome!\n\n"              << "1 : Easy\n"              << "2 : Normal\n"              << "3 : Hard\n\n"              << "Selection - ";    std::cin >> choice;          if(choice == difficulty.EASY)    {        std::cout << "\n\nDifficulty set to Easy\n\n";    }    if(choice == difficulty.NORMAL)    {        std::cout << "\n\nDifficulty set to Normal\n\n";    }    if(choice == difficulty.HARD)    {        std::cout << "\n\nDifficulty set to Hard\n\n";    }        //Waits till user terminates the program.    system("PAUSE");    return 0;}


Also note that I would have used a switch block instead of three if statements, but I didn't want to deviate too much from your current code and end up confusing you. Hope that helps you out.

-AJ
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
Just a guess:
try casting it to an int first.
std::cin >> (int) myDiff;

also this would be better:
    switch (myDiff)    {        case difficulty.EASY:            std::cout << "\n\nDifficulty set to Easy\n\n";            break;        case difficulty.NORMAL:            std::cout << "\n\nDifficulty set to Normal\n\n";            break;        case difficulty.HARD:            std::cout << "\n\nDifficulty set to Hard\n\n";            break;    }

Quote:
enum difficulty {EASY = 1, NORMAL, HARD};
Would make EASY, NORMAL, and HARD all equal to 1.

No, EASY is assigned value 1, and all other words that are not assigned a value explicitly, are assigned a value (1 + value of previous word)
So what i'm trying to say is that EASY=1, NORMAL=2 and HARD=3
Quote:Original post by u235
Well I don't know how it goes in C++, but in my prefered language(C#), this line of code:

enum difficulty {EASY = 1, NORMAL, HARD};

Would make EASY, NORMAL, and HARD all equal to 1.

Nope. It does make them 1, 2 and 3 respectively. In both languages.
Hack my projects! Oh Yeah! Use an SVN client to check them out.BlockStacker
Thanks for all the help. I got it working. I didnt know you couldnt use the enumeraotrs like variables... :D
Quote:Original post by staaf
Quote:Original post by u235
Well I don't know how it goes in C++, but in my prefered language(C#), this line of code:

enum difficulty {EASY = 1, NORMAL, HARD};

Would make EASY, NORMAL, and HARD all equal to 1.

Nope. It does make them 1, 2 and 3 respectively. In both languages.


My apologies. The way I it is the way I thought I read it in my C# reference. I just checked it though, and I do, indeed, stand corrected [smile].

-AJ
V/R,-AJThere are 10 kinds of people in the world: Those who understand binary and those who don't...
Quote:Original post by NUCLEAR RABBIT
Thanks for all the help. I got it working. I didnt know you couldnt use the enumeraotrs like variables... :D


You can, you just have to make sure that the functions and operators that you use it with support the enumator. If they don't exist, you write them yourself.

Steven Yau
[Blog] [Portfolio]

This topic is closed to new replies.

Advertisement