halogio
halogio
2016-01-13 07:09

C语言一个简单程序设计问题,找不出原因。

  • c
  • 结构数组
  • 函数返回值

这段程序是为了输入一个月份(英文单词),然后返回截止到输入月份的所有月的天数总和(比如我输入march,则返回1-3月份天数总和)。函数部分的功能是如果月份输入正确,就返回总数。如果输入不正确,那就返回-1。但是运行程序后,每次运行函数都是返回-1。不知道哪里出了问题,麻烦大家看下。谢谢。

代码如下:
#include
#include
#include

int days(char *p);
struct month{
char name[10];
char abbrev[4];
int days;
int monumb;
};

struct month months[12]={
{"january", "jan",31,1},
{"february", "feb",28,2},
{"march", "mar",31,3},
{"april", "apr",30,4},
{"may", "may",31,5},
{"june", "jun",30,6},
{"july", "jul",31,7},
{"august", "aug",31,8},
{"september", "sep",30,9},
{"october", "oct",31,10},
{"november", "nov",30,11},
{"december", "dec",31,12}
};


int main(void)
{
char input[10];
int daytotal;
printf("PLS enter the month");
while(fgets(input,100,stdin)!=NULL&&input[0]!='\0')
{
daytotal=days(input);
if(daytotal>0)
printf("The total days is %d.",daytotal);
else
printf("Input is not valid,pls enter again.\n");
puts("pls enter the next input");
}
return 0;
}


int days(char p) _*函数部分**_
{
int i=0;
int total=0;
while( p[i] != '\0' )
{
p[i] = tolower( p[i] );
i++;
}

 for(i=0;i<12;i++)
 {
 total += months[i].days ;
 if( strcmp( p, months[i].name) == 0 )
     return total;
 }
 return -1;

}

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答