Like Tree1Likes
  • 1 Post By Salem

Is the solution to the following exercise correct?

This is a discussion on Is the solution to the following exercise correct? within the C Programming forums, part of the General Programming Boards category; I've been stuck for hours trying to solve the following exercise: "Write a function called monthName that takes as its ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    11

    Is the solution to the following exercise correct?

    I've been stuck for hours trying to solve the following exercise:

    "Write a function called monthName that takes as its argument a value of type enum
    month (as defined in this chapter) and returns a pointer to a character string containing
    the name of the month. In this way, you can display the value of an enum month variable
    with a statement such as:

    printf (“%s\n”, monthName (aMonth));"

    I think I solved it, but the solution and the code doesn't feel right.

    Is there a simpler or better way to solve this exercise?

    Also, could you point out the errors in the code, if any.

    Code:
    #include <stdio.h>
    
    enum month { january = 1, february, march, april, may, june,
    	     july, august, september, october, november, december };
    
    int main (void)
    {
        enum month aMonth;
        char *monthName (enum month temp);
    
        printf ("%s\n", monthName (1));
        
        return 0;
    }
    
    char *monthName (enum month temp)
    {
        switch (temp) {
    	case january:
    	   return "January";
    	case february:
    	   return "February";
    	case march:
    	   return "March";
    	case april:
    	    return "April";
    	case may:
    	    return "May";
    	case june:
    	    return "June";
    	case july:
    	    return "July";
    	case august:
    	    return "August";
    	case september:
    	    return "September";
    	case october:
    	    return "October";
    	case november:
    	    return "November";
    	case december:
    	    return "December";
    	default:
    	    return "Please choose the correct value.";
        }
    }
    Last edited by ak22; 02-16-2012 at 12:10 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    It's not that bad as a first attempt.

    Perhaps this is more what you had in mind.
    Code:
    static char *monthNames[] = {
        "",
        "January",
        "February",
        // and so on
    };
    return monthNames[month];
    ak22 likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    11
    Quote Originally Posted by Salem View Post
    It's not that bad as a first attempt.

    Perhaps this is more what you had in mind.
    Code:
    static char *monthNames[] = {
        "",
        "January",
        "February",
        // and so on
    };
    return monthNames[month];
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. exercise help
    By ali.franco95 in forum C Programming
    Replies: 2
    Last Post: 09-24-2011, 11:02 AM
  2. Request for feedback -- exercise solution.
    By msh in forum C Programming
    Replies: 4
    Last Post: 07-07-2010, 12:48 PM
  3. Exercise C, or for, or while or do while
    By vanina_18 in forum C Programming
    Replies: 1
    Last Post: 06-15-2010, 03:20 PM
  4. Simple problem - my solution correct?
    By spadez in forum C Programming
    Replies: 15
    Last Post: 04-18-2009, 11:08 PM
  5. Help with K&R Exercise 1.13
    By Yannis in forum C Programming
    Replies: 2
    Last Post: 09-21-2003, 02:51 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21