Thread: need help with typedef enum

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    34

    need help with typedef enum

    hello my code is below I've pretty much finished what I need to do, however, I need to make my printDateInfo function output the name of the month instead of the number. So, I input 1 for the month when scanning and when the print function executes "January" should be printed instead of "1" like it is now.

    Code:
    // Compiler Includes
    #include <stdio.h>
    #include <string.h>
    
    //User defined definitions
    typedef enum {
    	JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC
    } MONTH_TYPE;
    
    typedef struct {
    	char make[20];
    	char model[20];
    	int odometer;
    } AUTO_TYPE;
    
    typedef struct {
    	int month;
    	int day;
    	int year;
    } DATE_TYPE;
    
    typedef struct {
    	double tankCap;
    	double curTankLevel;
    } TANK_TYPE;
    
    
    // Function Prototypes 
    void PrintCarInfo (AUTO_TYPE carToPrint);
    
    AUTO_TYPE ScanCarByValue(void);
    
    void PrintDateInfo(DATE_TYPE  ManDateToPrint, DATE_TYPE PurDateToPrint);
    
    DATE_TYPE ScanDateByValue(void);
    
    void PrintTankInfo(TANK_TYPE tankToPrint);
    
    TANK_TYPE ScanTankByValue(void);
     
    
    	// function main
    int main(void){ 
     
    	//Local variables
    	AUTO_TYPE carOne = {"\0", "\0",0};
    	DATE_TYPE carOneManDate = { 0,0,0};
    	DATE_TYPE carOnePurDate = { 0,0,0};
    	TANK_TYPE carOneTank = {0.0, 0.0};
    
      //begin
    	carOne = ScanCarByValue();
    
    	printf("enter the manufacture date\n");
    	carOneManDate = ScanDateByValue();
    	printf("enter the purchase date\n");
    	carOnePurDate = ScanDateByValue();
    	printf("enter the fuel measurements\n");
    	carOneTank = ScanTankByValue();
    
    	PrintCarInfo(carOne);
    	PrintDateInfo(carOneManDate, carOnePurDate);
    	PrintTankInfo(carOneTank);
    
    	return 0;
     
    } // end function main 
    
    
     
    void PrintCarInfo (AUTO_TYPE carToPrint) { 
      
    	printf("\n==================================================================\n");
      printf("MAKE: %s            MODEL: %s        Odometer: %d\n",carToPrint.make, 
             carToPrint.model, carToPrint.odometer);
    } 
    
    void PrintDateInfo (DATE_TYPE ManDateToPrint, DATE_TYPE PurDateToPrint) {
    	printf("\n------------------------------------------------------------------\n");
    	printf("MANUFACTURED ON: %d, %d, %d   PURCHASED ON: %d, %d, %d\n", ManDateToPrint.month,
    		ManDateToPrint.day, ManDateToPrint.year,PurDateToPrint.month,PurDateToPrint.day,
    		PurDateToPrint.year);
    }
    
    void PrintTankInfo(TANK_TYPE tankToPrint) {
    	printf("\nFUEL CAPACITY IS: %.2f GALLONS\t", tankToPrint.tankCap);
    	printf("CURRENT FUEL LEVEL IS: %.2f%%\n", ((tankToPrint.curTankLevel/tankToPrint.tankCap)*100));
    }
    
    
    AUTO_TYPE ScanCarByValue (void) { 
    
    	AUTO_TYPE inputCar = {"<NULL>", "<NULL>", 0};
    
    		printf("Enter car make: ");
    		scanf("%s", inputCar.make);
    		printf("Enter car model: ");
    		scanf("%s", inputCar.model);
    		printf("Enter odometer: ");
    		scanf("%d", &inputCar.odometer);
    
     return inputCar;
     
    } // end function ScanCarByValue 
    
     DATE_TYPE ScanDateByValue(void) {
    
    	 DATE_TYPE inputDate = {0,0,0};
    
    		printf("Enter month: ");
    		scanf("%d", &inputDate.month);
    		printf("Enter date:  ");
    		scanf("%d", &inputDate.day);
    		printf("Enter Year:  ");
    		scanf("%d", &inputDate.year);
    
    	return inputDate;
     }
    
     TANK_TYPE ScanTankByValue(void) {
    
    	TANK_TYPE inputTank = { 0.0, 0.0};
    
    		printf("enter tank capacity: ");
    		scanf("%lf", &inputTank.tankCap);
    		printf("enter current level: ");
    		scanf("%lf", &inputTank.curTankLevel);
    	
    	return inputTank;
     }
    
    // end file

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So where is your list of strings which you reference by the corresponding enum number?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    1
    Code:
    const char *month[] = {
              "January",
              //...
    };
    Make sure you change your printf() function according to what you declared your array as.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    i guess he has miss understood the way how enum works

    ssharish2005

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Read an enum tutorial, such as: http://www.cprogramming.com/tutorial/enum.html
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. Please STICKY this- vital to MSVC 6 dev - BASETSD.h
    By VirtualAce in forum Game Programming
    Replies: 11
    Last Post: 03-15-2005, 09:22 AM
  4. build errors migrated from dx9b to dx9c sdk
    By reanimated in forum Game Programming
    Replies: 4
    Last Post: 12-17-2004, 07:35 AM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM