c语言现代方法2th,自己编写的reminder.c程序找错

这是问题 

c语言现代方法2th,自己编写的reminder.c程序找错

下面的程序1.c是自己编写的,运行结果如下:

输入:

24 susan's birthday
5 6:00 - Dinner with Marge and RUSS 
25 Movie - "Chinatown"
7 10:30 - Dental appointment
5 Saturday
0

输出:

 24 Saturday

 问题:程序1.c错在哪里了?

//程序1.c
#include <stdio.h>
#include <string.h>

#define MAX_REMINDERS 50
#define MSG_LEN 60

int read(char (*p)[MSG_LEN + 3],int n);
int readline(char *,int n);
void print(char (*p)[MSG_LEN + 3]);
int main()
{
char reminders[MAX_REMINDERS + 1][MSG_LEN+ 3];//2 seat for date,others for reminders
read(reminders,MAX_REMINDERS + 1);
print(reminders);
    return 0;
}

int read(char (*p)[MSG_LEN + 3],int n)
{
//用整数输入日期,一会利用sprintf转换成char *
int day,i,j;
char remind[MSG_LEN + 1];
int count  =  0;
//there is a space character after the day
char day_str[4];
scanf("%2d",&day);
readline(remind,MSG_LEN + 1);
//转换day,使得它成为char *,sprintf的功能是把格式化字符串打印到字符串中,
//sprintf第一个就是要打印的目的字符串(打印到哪),其余的和以前的printf一样
sprintf(day_str,"%3d",day);
while(day != 0)
 { 

   for(i = 0;i < count; ++i)
    {
       if(strcmp(p[i],day_str) > 0) 
          break;        
    }
 
  for(j = count - 1;j >= i;--j)
  {
     strcpy(p[j+1],p[j]); 
  }
  strcpy(p[i],day_str);
  strcat(p[i],remind);
  ++ i;
  strcpy(p[i],"");
  count ++;
  
  scanf("%2d",&day);
  readline(remind,MSG_LEN + 1);
 }


    return count;
}//while

int readline(char *p,int n)
{
char ch;
int  cnt = 0;
while((ch = getchar()) != '\n' && cnt < n)
  { 
    *p ++ = ch;
    ++ cnt;
  }
 *p = '\0';
 return cnt;
}

void print(char (*p)[MSG_LEN + 3])
{
for(; strcmp(*p,"") != 0; ++p)
  {
     printf("%s\n",*p);
  }
}

 

上一篇:Codeforces Round #540 (Div. 3) 部分题解


下一篇:CF1033G Chip Game