结构体输入的数量不确定的情况,如何写?
程序代码:
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #define N 100 typedef struct info { char name[LENGTH]; int age; }INFO; void input(INFO people[]) { int i, amount=0; char ch[2]; puts("Plz enter some sensetive data concerning people u know(at least 3 people)."); for(i=0; i < 3; i++) { printf("Plz enter this person's name: "); scanf("%s", people[i].name); printf("Plz enter this person's age: "); scanf("%d", &people[i].age); } for(i=3; i < N; i++) { printf("Would u like to enter another person? (y/n) "); scanf("%s", ch); if(strcmp(ch, "n") == 0) break; else if(strcmp(ch, "y") == 0) { printf("Plz enter this person's name: "); scanf("%s", people[i].name); printf("Plz enter this person's age: "); scanf("%d", &people[i].age); } } amount = sizeof(people) / sizeof(INFO); //加上这行,输入完,未打印结构体信息 printf("Name Age\n"); printf("---------------------------\n\n"); for(i=0; i < amount; i++) { printf("%-24s%d\n", people[i].name, people[i].age); } } int main(void) { INFO people[N]; input(people); return 0; }
这个程序会要求职时输入3组人的信息,多于3组有提示,我的问题就是,比如我要输入4组,这个amount,我这个代码貌似有错amount = sizeof(people) / sizeof(INFO); //加上这行,输入完,未打印结构体信息如何定?或者有没有其他办法修改我这个代码
请高手指点,谢谢