#include<stdio.h>
#include<string.h>
#define LEN 20
#define max 5
char* s_gest(char* st, int n);
void getinfo(struct insurance fond[]);
void showinfo(struct insurance str[]);
struct name {
char first[LEN];
char middle[LEN];
char last[LEN];
};
struct insurance{
struct name handle;
int number;
};
int main(void) {
struct insurance people[max];
printf("请在开始时用换行结束输入\n");
puts("输入名");
getinfo(people);
showinfo(people);
}
char* s_gets(char* st, int n) {
char* ret_val;
char* find;
ret_val = fgets(st, n, stdin);
if (ret_val) {
find = strchr(st, '\n');
if (find)
*find = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
void getinfo(struct insurance fond[]) {
int count = 0;
while (count < max && s_gets(fond[count].handle.first, LEN) != NULL && fond[count].handle.first[0] != '\0') {
puts("输入中间名");
s_gets(fond[count].handle.middle, LEN);
puts("输入姓");
s_gets(fond[count].handle.last, LEN);
puts("保险号");
scanf_s("%d", fond[count].number);
while (getchar() != '\n')
continue;
count++;
if(count<max)
puts("输入名");
}
}
void showinfo(struct insurance str[]) {
int count = 0;
while (count < max) {
printf("%20s,", str[count].handle.first);
if (gets_s(str[count].handle.middle, max - 1) != NULL) {
printf("%20s.", str[count].handle.middle);
}
printf("%20s,", str[count].handle.last);
printf("%d,", str[count].number);
}
}
代码第五行函数名应该是s_gets
在函数 getinfo 中,scanf_s("%d", fond[count].number); 应该改为 scanf_s("%d", &fond[count].number);,因为 scanf_s 函数需要传递变量的地址
在函数 showinfo 中,gets_s 函数已经被弃用,应该使用 fgets 函数来读取字符串。另外,gets_s 函数的第二个参数应该是 LEN 而不是 max - 1
在函数 showinfo 中,缺少了 count++ 语句,导致循环无法结束
改了之后再试试
46行缺&