Thread: Enum and how to use it

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105

    Enum and how to use it

    Hello there!

    I recently used an enum for the first time and I haven't figured out a solution for my problem. What I tried to do was retrieve the actual computer's date, get the month in a string and then turn into a number (through the enum) but I can't find a way to do so, since everytime I try printing the month I get 0. I tried the following:

    Code:
    enum Months { dummy, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; //dummy is just a temporary solution for 0
    
    string date = getDate(); //"Dec/2013"
    string month = getMonth_str(date); //month = "Dec"
    cout << month; //output: 0; Shouldn't it be 12?
    Thanks in advance
    Last edited by Khabz; 12-28-2013 at 08:49 AM.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Are you expecting us to be psychic as to what getMonth_str() does?

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    I think it's pretty understandable if you actually take a look at the code.
    Anyway, it receives a parameter (string date) in the format MM/AA and retrieves the month of that date (MM). As you can see in the comments for the string "Dec/2013" it outputs "Dec".

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Give us the code to the functions so we can find your error. I'm sure you can see as we can see that the 5 lines of code you pasted is not the issue. Post your full solution if you want some help.

  5. #5
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Code:
    string getDate() { //Returns date in the format Month/Year
    
    
        string year; string month;
        // mes e ano actuais
        time_t now = time(NULL);
        string dt = ctime(&now);
    
    
        istringstream iss(dt);
        for (unsigned i = 0; i < 5; ++i) {
            getline(iss,year,' ');
            if (i == 1)
                month = year;
        }
        ostringstream oss;
        oss << month << "/" << year;
        return oss.str();
    }
    
    string getMonth_str(const string& date) {
    
    
        istringstream ss(date);
        string month; getline(ss,month,'/');
        return month;
    }
    
    
    void function() {
    
         enum Months { dummy, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec }; //dummy is just a temporary solution for 0
    
         string date = getDate(); //"Dec/2013"
         string month = getMonth_str(date); //month = "Dec"
         cout << month; //output: 0; Shouldn't it be 12?
    }

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    In the above snippets where do you actually try to use your enumeration?

    Why do you expect the string month to output any number?

    Shouldn't it just return the month as a string ("Dec")?


    Jim

  7. #7
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    That's what I'm having troubles with. I thought that by having the string "Dec" or of any other month, by printing it I'd get the number it's related to in the enum, but I realised otherwise.

    Code:
    string month = getMonth_str(date); //month = "Dec"
    cout << month
    This simply and reasonably outputs the string itself, but I want to be the same as

    Code:
    cout << Dec; //output: 12
    and I'm a bit stuck with this

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Then I suggest you don't need an enumeration. Think about using a vector/array to hold the months. Then iterate through this array and compare the current month to each element of the array, when you get a match you know the month number (the index).

    Jim

  9. #9
    Registered User
    Join Date
    Mar 2013
    Location
    Portugal, Porto.
    Posts
    105
    Yeah that was my temporary solution, I guess Imma just stick with that, sounds like a better solution.

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    I really think you may want to find and read more about enum. You really don't seem to understand the concept.


    Code:
     
         enum Months { Jan = 1, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec };
         string months[] = {"dummy", "Jan", "Feb", "Mar", "Apr", "May", "Jun",
                            "Jul", "August", "Sep", "Oct", "Nov", "Dec"};
         cout << months[Aug] << endl;

    Jim

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    While you're at it, be sure to take a look at the C++11 enums.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe with enum. Assigning a char to an enum value.
    By Iceboxes in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2013, 09:58 AM
  2. Assigning enum types to enum types?
    By see the big C in forum C Programming
    Replies: 10
    Last Post: 12-21-2010, 02:32 AM
  3. enum
    By mouse666666 in forum C Programming
    Replies: 2
    Last Post: 03-30-2010, 12:17 AM
  4. Enum
    By Ducky in forum C++ Programming
    Replies: 4
    Last Post: 03-21-2010, 03:59 AM
  5. Enum
    By swgh in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2005, 11:55 AM