Thread: Getting error using an enum in an if statement???

  1. #1
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20

    Angry Getting error using an enum in an if statement???

    Hi, i'm trying to make a simple C++ program in which the user must try to guess a number, if they guess too high it says "too high" and if they guess too low it says "too low".
    I also decided to add a feature which allows them to select how many tries they would like to guess the number. I tried to make "tries" type an enum so if the user could not pick an invalid number but for some reason i cannot use it in an if statement.

    here is the code and i am getting the first error on line 27:

    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    
    using namespace std;
    
    
    int guess;
    int i;
    int endProgram = 0;
    int win = 0;
    int j = 0;
    enum tries
    {
      five = 5,
      ten = 10,
      fifthteen = 15,
      twenty = 20
    };
    
    
    int getValue()
    {
        srand(time(NULL));
        i = rand() % 100 + 1;
    }
    int finish()
    {
        if (guess == i || j == tries)
        {
            endProgram = 1;
        }
        else if ( guess > (i + 10) && guess < (i + 50) && guess > (i - 50))
        {
            cout <<"\n\n\n\nTOO HIGH\n\n\n\n";
        }
        else if (guess < (i - 10) && guess < (i + 50) && guess > (i - 50))
        {
            cout <<"\n\n\n\nTOO LOW\n\n\n\n";
        }
        else if (guess >= (i - 10) && guess < i)
        {
            cout <<"\n\n\n\nA LITTLE TOO LOW\n\n\n\n";
        }
        else if (guess >= (i + 10) && guess > i)
        {
            cout <<"\n\n\n\nA LITTLE TOO HIGH\n\n\n\n";
        }
        else if (guess >= (i + 50))
        {
            cout <<"\n\n\n\nWAY TOO HIGH\n\n\n\n";
        }
        else if (guess <= (i - 50))
        {
            cout <<"\n\n\n\nWAY TOO LOW\n\n\n\n";
        }
    }
    
    
    int main()
    {
        char retry;
    
    
    getValue();
    cout <<"HOW MANY ATTEMPTS WOULD YOU LIKE (5, 10, 15, 20): ";
    cin >>tries;
    cout <<"\n\n\n\n";
        while (endProgram != 1)
        {
            cout <<"TAKE A GUESS: ";
            cin >>guess;
            finish();
            j++;
        }
        if (guess == i)
        {
            cout <<"CONGRATULATIONS YOU GUESSED THE NUMBER IN " <<j <<"/" <<tries <<" TRIES!";
        }
        else
        {
            cout <<"UNLUCKY, YOU FAILED TO GUESS THE NUMBER"
        }
    
    
        cout <<"\n\nPLAY AGAIN? (y/n)";
        cin >>retry;
        if (retry == 'y')
        {
            return main();
        }
    }
    Could someone please explain to me what I am doing wrong here?

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    "tries" is the name of the type, just like "int"; it is not itself a variable.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Tabstop has nailed the main problem.

    I'll also point out that fifteen is not spelt with an 'h'. Doesn't matter to the compiler, but does affect human comprehension.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20
    haha my bad

  5. #5
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20
    ok thanks that solved my problem

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, you should avoid global variables. You should declare variables near first use.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User Grae's Avatar
    Join Date
    Sep 2013
    Posts
    20
    what if i want to use a variable in multiple functions?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Grae
    what if i want to use a variable in multiple functions?
    Pass appropriate arguments to those functions that need them.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. typedef enum statement confusion.
    By Daniel.Blesener in forum C Programming
    Replies: 4
    Last Post: 06-19-2012, 01:57 PM
  2. enum error
    By robi in forum C Programming
    Replies: 2
    Last Post: 10-14-2011, 07:17 PM
  3. Odd enum error...
    By tjpanda in forum C++ Programming
    Replies: 2
    Last Post: 09-13-2008, 03:08 AM
  4. compiler error with global enum
    By Mark S. in forum C++ Programming
    Replies: 3
    Last Post: 08-31-2006, 08:15 AM
  5. Help on an enum statement
    By Blanket in forum C++ Programming
    Replies: 1
    Last Post: 03-21-2003, 09:45 PM

Tags for this Thread