-
enum code problem
Greetings,
My brain is inoperable and I need a fresh set of eyes to hint me in the right direction? Any help to completing the code and additional help to start a day and year functions would help?
Code:
#include<iostream>
#include<string>
using namespace std;
enum month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC};
void months();
int main()
{
cout << "Enter the month\n";
string M;
cin >> M;
cout << M;
return 0;
{
void months()
{
switch (M)
{
case JAN:
cout << "January";
case FEB:
cout << "February";
case MAR:
cout << "March";
case APR:
cout << "April";
case MAY:
cout << "May";
case JUN:
cout << "June";
case JUL:
cout << "July";
case AUG:
cout << "August";
case SEP:
return "September";
case OCT:
cout << "October";
case NOV:
cout << "November";
case DEC:
cout << "December";
default:
cerr << "\n***TRY AGAIN!***\n";
return M;
}
}
Any help is appreciated??? I stink at enumerators!!!!
cj
-
u lost me
i'm sorry maybe ur tired.. and c++ is driving u to insanity but i don't understand ur code.. u never call the function
void months();
and ur main function doesn't end correctly
also u have to pass the string into the function months for ur program to workk..
Code:
void months(month);
int main()
{
cout << "Enter the month\n";
month M;
cin >> M;
months(M);
cout << M;
return 0;
}
void months(month M)
{
switch (M)
{
case JAN:
cout << "January";
break;
case FEB:
cout << "February";
break;
case MAR:
cout << "March";
break;
case APR:
cout << "April";
break;
case MAY:
cout << "May";
break;
case JUN:
cout << "June";
break;
case JUL:
cout << "July";
break;
case AUG:
cout << "August";
break;
case SEP:
cout << "September";
break;
case OCT:
cout << "October";
break;
case NOV:
cout << "November";
break;
case DEC:
cout << "December";
break;
default:
cerr << "\n***TRY AGAIN!***\n";
break;
}
return ;
}
i think u need breaks after each CASE statement as well :)
Also u declare and enum type of MONTH but u never created a datatype.. i believe.. not sure i don't use enum types much good luck with programm.. hope this helped
-
You needed some help
Code:
#include<iostream>
#include<string>
#include <conio.h>
using namespace std;
enum month {JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC};
int main()
{ month mn;
int M;
cout << "1: january ,2: Feb, 3: March, 4 :april, 5 may etc..";
cin >>M;
switch (M)
{
case 1:
cout << "January";
break;
case 2:
cout << "February";
break;
case 3:
cout << "March";
break;
case 4:
cout << "April";
break;
case 5:
cout << "May";
break;
case 6:
cout << "June";
break;
case 7:
cout << "July";
break;
case 8:
cout << "August";
break;
case 9:
cout << "September";
break;
case 10:
cout << "October";
break;
case 11:
cout << "November";
break;
case 12:
cout << "December";
break;
default:
cerr << "\n***TRY AGAIN!***\n";
break;
}
getch();
return 0;
}
-
that would work but
the above code would work but using integers in the switch statements defeats the purpose of creating an enum datatype..
and this line
does nothing..
if u wanted to use integers u could just do this..
Code:
month m;
int num;
cout << "Enter a number: ";
cin >> num;
m = (month)num;
or something to that affect.. not sure if above is correct or below is.
Code:
month m;
int num;
m = num;
enum isn't on my brain right now.. maybe someone else could explain in greater detail
Code:
enum month{jan = 0, feb = 1, etc...}
in case u don't know how enum works look at above statement.. u can either reference it as Jan or Feb or by using its indice 0 or 1 0 & Jan are the same thing :P
have a nice day :D
-
I'll tell ya, easily:
enum is just as if you created a bunch of (int)s with sequential values.
so enum {JAN,FEB,MAR}; is equal to
int JAN=0, FEB=1, MAR=2; //Get it, only enum is not an int it's it's own data type
you can assign an enum value to an INT but you must cast and INT value to an enum.
Do this in your code:
enum month {JAN=1,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC };
It'll set JAN to 1 and FEB to 2 and so on, otherwise the count starts at 0.
Others posted code so I wasn't going to but I worked on it so here it is, I didn't finish it but you should be able to fill in the blanks.
Code:
#include <iostream>
using namespace std;
enum month {JAN=1,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC};
void months(int);
int main(void){
cout << "Enter the month as 1 for January, 2 for Feburary, etc...\n";
int M;
cin >> M;
months(M);
return 0;
}
void months(int M)
{
switch (M)
{
case JAN:
cout << "January"<<endl;
break;
case FEB:
cout << "February"<<endl;
break;
case MAR:
cout << "March";
break;
case APR:
cout << "April";
break;
case MAY:
cout << "May";
case JUN:
cout << "June";
case JUL:
cout << "July";
case AUG:
cout << "August";
case SEP:
cout<< "September";
case OCT:
cout << "October";
case NOV:
cout << "November";
case DEC:
cout << "December";
default:
cerr << "\n***TRY AGAIN!***\n";
}
}