函数使用字符串传递If语句?

时间:2017-04-29 19:08:24

标签: c string function strcmp

字符串初始化:

main(){
char monthName[20];
int days;

printf("Enter the month: ");
fgets(monthName, 20, stdin);

days = monthCalc(monthName);
}

功能:

int monthCalc(char monthName[20]) {
int days = 0;

if (strcmp(monthName, "January") == 0)
    days = 31;
else if (strcmp(monthName, "February") == 0)
    days = 28;
else if (strcmp(monthName, "March") == 0)
    days = 31;
else if (strcmp(monthName, "April") == 0)
    days = 30;

例如,如果我输入January,则char数组会收集字符串“January”。但是当在monthCalc函数中使用strcmp进行比较时,天数不会像它应该的那样得到31。谢谢你的帮助!!!

0 个答案:

没有答案