-
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
-
Are you expecting us to be psychic as to what getMonth_str() does?
-
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".
-
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.
-
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?
}
-
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
-
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
-
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
-
Yeah that was my temporary solution, I guess Imma just stick with that, sounds like a better solution.
-
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
-
While you're at it, be sure to take a look at the C++11 enums.