为什么vs2015运行提示读取字符串的字符时出错,而devcpp正常运行?

zjda42 2017-04-01 12:07:30
程序如下:(vs中字符串输入已改为_s 版本的)


/* 打印一个月的日程安排*/

#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;
}
...全文
515 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
加上预编译头文件应该就不会在编译的时候报错了
#include <stdafx.h>
另外,这段程序无论在哪儿跑,可能会出现运行时发生冲突,那是指针的问题。

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧