Thread: enum code problem

  1. #1
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    Question 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
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    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
    Last edited by tegwin; 11-15-2002 at 10:19 PM.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    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;
    }

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    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
    Code:
    month mn;
    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
    Last edited by tegwin; 11-15-2002 at 10:35 PM.

  5. #5
    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";
    		
    	} 
    }
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with compiling code with option -O2
    By koushikyou in forum C Programming
    Replies: 16
    Last Post: 01-07-2009, 06:03 AM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Replies: 5
    Last Post: 09-28-2004, 12:38 PM
  4. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM