Help with a function.

This is a discussion on Help with a function. within the C++ Programming forums, part of the General Programming Boards category; I'm in need of some help here, i have a function that is supposed to return the name of the ...

  1. #1
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964

    Help with a function.

    I'm in need of some help here, i have a function that is supposed to return the name of the current month.

    When i use the <ctime> header and SYSTEMTIME, i get the current month as a number, so i wrote a function to return the name of the month:

    Code:
    #ifndef MONTH_H
    #define MONTH_H
    
    #include <ctime>
    
    void monthcheck(char * currentmonth[10])
    {
        SYSTEMTIME time;
        GetSystemTime(&time);
        switch (time.wMonth)
        {
               case 1:
               *currentmonth = "January";
               break;
               case 2:
               *currentmonth = "February";
               break;
               case 3:
               *currentmonth = "March";
               break;
               case 4:
               *currentmonth = "April";
               break;
               case 5:
               *currentmonth = "May";
               break;
               case 6:
               *currentmonth = "June";
               break;
               case 7:
               *currentmonth = "Juli";
               break;
               case 8:
               *currentmonth = "August";
               break;
               case 9:
               *currentmonth = "September";
               break;
               case 10:
               *currentmonth = "October";
               break;
               case 11:
               *currentmonth = "November";
               break;
               case 12:
               *currentmonth = "December";
               break;
        }
    }
    
    #endif
    Now, if i write the following code in my main program:

    Code:
    char currentmonth[10];
    char (*pcurrentmonth)[10] = &currentmonth;
        
    monthcheck(pcurrentmonth);
    I get the compile error: cannot convert `char (*)[10]' to `char**' for argument `1' to `char monthcheck(char**)'

    I'm not very good with pointers, but Google told me that this is the way it should look, am i right?

    I've tried everything, what am i doing wrong?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >void monthcheck(char * currentmonth[10])
    currentmonth should be declared as an array of char:
    Code:
    void monthcheck(char currentmonth[])
    > *currentmonth = "January";
    Use strcpy to copy to char arrays:
    Code:
               strcpy(currentmonth,  "January");
    Better yet use strings instead of a char arrays:
    Code:
    #include <string>
    .
    void monthcheck(std::string &currentmonth)
    {
    .
    .
               currentmonth = "January";
    Last edited by swoopy; 10-06-2006 at 03:32 PM. Reason: made currentmonth a string reference

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    What you are doing isn't going to work. First of all, the currentmonth argument to monthcheck is an array of pointers to char. However, you are trying to pass a pointer to an array of ten char. You could change the prototype of your function to this:
    Code:
    void monthcheck(char (*currentmonth)[10]) //pointer to an array of 10
    //...
    //to call the function:
    char currentmonth[10];
    monthcheck(&currentmonth);
    And you'll find the errors change It's also probably unnecessary to pass a pointer.

    If you want to store those month string into an array, you need to use strcpy or a similar function.
    Code:
    void monthcheck(char currentmonth[10])
    {
    //...
    strcpy(currentmonth,"January");
    }
    Edit: And I agree with swoopy, in C++ the std::string class is usually preferable to c-style strings.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You have a precedence error. What you have
    Code:
    void monthcheck(char * currentmonth[10])
    isn't the same as
    Code:
    void monthcheck(char ( * currentmonth ) [10])
    In any case, since you don't need to change char currentmonth points to, you can just use this:
    Code:
    void monthcheck(char currentmonth[10])
    or the equivalent
    Code:
    void monthcheck(char * currentmonth)
    To copy a C-string to another, you need to use strcpy (in <cstring>). Assignment doesn't work too well.
    Code:
    #include <cstring>
    // ...
    void monthcheck(char * currentmonth) {
        // ...
               case 1:
               strcpy(currentmonth, "January");
               break;
    You'll also need to use std::strcpy unless you have a using directive.
    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.

  5. #5
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Thanks guys, its working like a charm now.

    I need to read more about pointers i guess.

    Another small question:

    Is it possible to make a program ignore upper/lower case of the user input. Instead of having to write all my code twice?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can convert the entire input to uniform case just after reading it in and only work on that.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    tolower() or toupper() would be useful for that purpose.
    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. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM

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