33,311
社区成员
发帖
与我相关
我的任务
分享
/* 打印一个月的日程安排*/
#include<iostream>
#include<stdlib.h>
#include<string.h>
#define MAX_REMIND 50
#define MSG_LEN 60
using std::cout;
using std::cin;
using std::endl;
int read_line(char str[], int n);
int main()
{
char *reminders[MAX_REMIND];
char day_str[3], msg_str[MSG_LEN + 1];
int day, i, j, num_remind = 0;
for (;;) {
if (num_remind == MAX_REMIND) {
cout << "--No space left--" << endl;
break;
}
printf("Enter day and reminder:");
cin >> day;
if (day == 0)
break;
sprintf_s(day_str,100, "%2d", day);
read_line(msg_str, MSG_LEN);
for (i = 0; i < num_remind; i++)
if (strcmp(day_str, reminders[i]) < 0) //reminders[i]提示读取出错
break;
for (j = num_remind; j > i; j--)
reminders[j] = reminders[j - 1];
reminders[i] = (char*)malloc(2 + strlen(msg_str) + 1);
if (reminders[i] == NULL) {
cout << "--No space left--" << endl;
break;
}
strcpy_s(reminders[i], 100, day_str);
strcat_s(reminders[i], 100, msg_str);
num_remind++;
}
cout << endl << "Day Reminder" << endl;
for (i = 0; i < num_remind; i++)
cout << " " << reminders[i] << endl;
return 0;
}
int read_line(char str[], int n)
{
int ch, i = 0;
while ((ch = getchar()) != '\n')
if (i < n)
str[i++] = ch;
str[i] = '\0';
return i;
}
#include <stdafx.h>
另外,这段程序无论在哪儿跑,可能会出现运行时发生冲突,那是指针的问题。