【发布时间】:2014-12-05 08:16:44
【问题描述】:
我正在使用一个程序(非常简单),它需要用户使用 scanf 进行输入。这些值存储在结构数组中。无论如何,我认为我的所有语法都正确(如果我不纠正我的话),但是每当我想 printf 查看结果时,我都没有从 ID[i].Middle_Name 获得值。由于我不明白的原因,它似乎是空的。我试图添加一个打印语句来尝试调试它,但仍然没有。也许我错过了什么?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
struct Students{
char First_Name[20];
char Last_Name[20];
char Middle_Name[20];
int Age;
};
int main(void){
int n = 0; //number of students
int i = 0; //counter
printf("Please enter the total number of students in your class: ");
scanf("%d", &n);
struct Students ID[n];
printf("\n");
printf("******************** \n");
printf("After entering student info, they are displayed in full_name/middle_name/age order. \n");
printf("******************** \n");
for(i = 1; i <= n; i++){
printf("Please enter the first name of student with ID: %d \t", i);
scanf("%s", (ID[i].First_Name));
printf("\n");
printf("Please enter the last name of student with ID: %d \t", i);
scanf("%s", (ID[i].Last_Name));
printf("\n");
printf("Please enter the middle name of student ID: %d \t", i);
scanf("%s", (ID[i].Middle_Name));
printf("\n");
printf("Please enter the age of student ID: %d \t", i);
scanf("%d", &(ID[i].Age));
printf("\n");
}
printf("In your class, we have: \n");
printf("%s", ID[1].Middle_Name);
for(i = 1; i <= n; i++){
printf("%s \t", ID[i].First_Name);
printf("%s \t", ID[i].Last_Name);
printf("%s \t", ID[i].Middle_Name);
printf("%d \n", ID[i].Age);
}
return(0);
}
【问题讨论】:
-
你的输入格式是什么?
-
C 中的起始索引为 0 而不是 1,因此您的 for 应以 i=0 开头