-
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] = ¤tmonth;
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?
-
>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 ¤tmonth)
{
.
.
currentmonth = "January";
-
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(¤tmonth);
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.
-
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.
-
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?
-
You can convert the entire input to uniform case just after reading it in and only work on that.
-
tolower() or toupper() would be useful for that purpose.