converting month number to month name

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • girl23
    New Member
    • Oct 2007
    • 3

    converting month number to month name

    I am using a function prototype to convert month number to month name.
    basically you ask the compiler to enter int from 1 to 12 and then convert it.

    printMonth ( number ); is the prototype and using switch this is what i did

    case 'm':
    printf( "What month? Enter a number between 1 and 12: " );
    scanf( "%d%c", &number, &tmp );
    printMonth( number );
    break;
    But i just don't know how to call the function . can someone help.
  • simplebelle
    New Member
    • Oct 2007
    • 5

    #2
    Originally posted by girl23
    I am using a function prototype to convert month number to month name.
    basically you ask the compiler to enter int from 1 to 12 and then convert it.

    printMonth ( number ); is the prototype and using switch this is what i did

    case 'm':
    printf( "What month? Enter a number between 1 and 12: " );
    scanf( "%d%c", &number, &tmp );
    printMonth( number );
    break;
    But i just don't know how to call the function . can someone help.
    I think the best way you need to use is switch case statement:
    here the syntax:
    switch(variable _expression){
    case constant_value;
    {
    statement 1;
    break;
    }
    case constant_value;
    {
    statement 2;
    break;
    }
    statement 3;
    break;
    }
    case constant_value;
    {
    statement 4;
    break;
    }

    (until 12)
    default;
    satemen 13
    break;
    }

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by girl23
      case 'm':
      printf( "What month? Enter a number between 1 and 12: " );
      scanf( "%d%c", &number, &tmp );
      printMonth( number ); <<<<<<<this is your call
      break;
      But i just don't know how to call the function . can someone help.
      You just called the function.


      What am I missing??

      Comment

      • girl23
        New Member
        • Oct 2007
        • 3

        #4
        Originally posted by simplebelle
        I think the best way you need to use is switch case statement:
        here the syntax:
        switch(variable _expression){
        case constant_value;
        {
        statement 1;
        break;
        }
        case constant_value;
        {
        statement 2;
        break;
        }
        statement 3;
        break;
        }
        case constant_value;
        {
        statement 4;
        break;
        }

        (until 12)
        default;
        satemen 13
        break;
        }
        Thank you so much
        that helps me a lot

        Comment

        • Prasannaa
          New Member
          • May 2007
          • 21

          #5
          Hi,

          Another simple method. Since month names are not going to change, you can create a static array with month names and just index (after checking for bounds) the array with the number.
          Now it becomes a simple array index.


          Prasannaa

          Comment

          • jwwicks
            New Member
            • Oct 2007
            • 19

            #6
            So something like the following would work...

            [code="cpp"]
            static char m_months[13][10] = {"", "January", "February", "March", "April", "May", "June", "July", "August", "September" , "October", "November", "December"} ;
            string month(m_months[n]);
            [/code]

            Where n is the number entered from the user...

            John

            Comment

            Working...