Help with enumeration and static casting

Hi,

my name is shane. I am just learining c++. I am having a problem on my homework getting the correct answer to output. I need the input of the date (01/01/2011) to output Janury/01/2011. I cannot get the number of the month to convert to the word month (i.e., 01 to January. Below is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#include <iostream>

using namespace std;

//Declare enumeration type

enum monthType {January = 1, Februaury = 2, March = 3, April = 4, MAy = 5, June = 6,
           July = 7, August = 8, September = 9, October = 10, November = 11, December = 12};


int main()
{

//Declare variables

monthType monthTypeVariable;
int intVariable, num2, num3;
char  ch1, ch2;

//Retrieve date

    cout << "enter date in mm/dd/yyyy: ";
    cin >> intVariable >> ch1 >> num2 >> ch2 >> num3;
    cout << endl;

//convert month number to month word

monthTypeVariable=static_cast<monthType>(intVariable);

//output date

cout<< "date is " << monthTypeVariable <<ch1 << num2 << ch2 << num3;
cout << endl;

return 0;
}


The problem requires enumeration and static casting within the code. When I run this program, the date of the month remains a number instead of changing to the name of the month



enter date: 01/01/2001

date is 1/1/2011

.

Thanks for your help.
Last edited on
Enums are printed as integers unless you specify a custom output operator.
For what you are trying to do, an array of strings is better than an enum,
so you just print array_of_month_names[intVariable]
You have to convert it. You could use a switch:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

enum Fruit {
     Apple,
     Banana,
     Cherry
};

int main() {
    Fruit my_favorite = Banana;
    std::cout << "My favorite fruit is: ";
    switch( my_favorite )
    {
        case Apple:  std::cout << "Apple"; break;
        case Banana: std::cout << "Banana"; break;
        case Cherry: std::cout << "Cherry"; break;
    }
    std::cout << std::endl;
}
Last edited on
Bazzy: I hear arrays make programs so much easier to code but I don't know how to use arrays. My level of coding has not reached this level. Plus my professor would know this with previous entry of homework. Also, the homework requires I use enumeration to solve the problem.

jsmith: If I can't figure it out with static_casting then I will turn to switch and get whatever credit I can from it. The problem requires I use "static casting ". I thought static casting does convert variables?
make programs so much easier to code

Rather, they (data collections in general) make the solution of a broad number of problems possible in the first place.

I thought static casting does convert variables?


Yes they it does, but it doesn't work the way you think it does. A static_cast (or any other cast for that matter) isn't even remotely going to help you, if you can't use arrays a switch is the best solution.
Last edited on
Your enum does not have a operator<< for ostream, this means that it will be casted back to an integer when you try to output it with cout<< .

Enum values are nothing more than integer constants. If you try cout << January it will print 1.
Topic archived. No new replies allowed.