Thread: Question for return type.

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    54

    Question for return type.

    Hi all;

    I want to write a function that gets input from the user, between 1-12, and returns by name of month. For example, if the user input 1, then I should return January, and so on. I know that I should use the case...switch for this function, but I don't know how to return a string function. Any help would be appreciate.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    how about using a switch...case after returning, that way, u can just return an int

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    I don't get your point. Please explain a little bit more. Again, the requirement of the function is returning the name of the month. Thanks!

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Code:
    char* getmonthfuction (int userinput)
    {
    switch (userinput)
               {
                case 1:
                          return ("Janurary");
                .....
               }
    }

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    57
    A standard practice in such cases is using 'enum' 's. You can look up for it in any book or google.

    Anoop.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Another alternative is tables:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char *getMonth(unsigned imonth)
    {
        static char *months[] = {
            "",
            "January",
            "February",
            "March",
            "April",
            "May",
            "June",
            "July",
            "August",
            "September",
            "October",
            "November",
            "December"
        };
    
        return (imonth > 12) ? "" : months[imonth];
    }
    
    int main(void)
    {
        char     buff[BUFSIZ];
        unsigned n;
    
        printf("Enter a number (1-12): ");
        fflush(stdout);
    
        if (fgets(buff, sizeof buff, stdin) != NULL) {
            n = (unsigned)strtoul(buff, NULL, 0);
            puts(getMonth(n));
        }
    }
    My best code is written with the delete key.

  7. #7
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    why would you use such a large buffer for such a small input of data?

    (On the current system I am using, BUFSIZ is 512)
    but buffer of size 8 would work just fine right?

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >why would you use such a large buffer for such a small input of data?
    My current plan is to confuse you. Then I'll use gets and confuse you even more. When you're thoroughly confused, I'll make subtle suggestions that will eventually break your mind and make you my slave.
    My best code is written with the delete key.

  9. #9
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    LOL, oh I see...

    but you don't need to make me your slave, you got such mad programming skillz, you don't need slaves, you could program them.

    and besides, I'm alread someone else's

    -LC
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    Thank you all!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. strings Vs. Char pointers
    By aijazbaig1 in forum C Programming
    Replies: 49
    Last Post: 02-13-2008, 09:51 AM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. Newbie question about the Private type in classes
    By Ryeguy457 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2002, 10:17 PM