我有一個我正在使用的程序(安靜簡單),需要使用scanf的用戶輸入。這些值存儲在一個結構數組中。無論如何,我認爲我的所有語法正確無誤(如果我不請正確的話),但是每當我想要printf查看結果時,我都沒有從ID [i] .Middle_Name獲取值。看起來好像是空的,原因我不明白。我試圖添加一個打印語句來嘗試和調試它,但仍然沒有。也許我錯過了什麼?scanf問題()
#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);
}
什麼是你的輸入格式? – timrau 2014-12-05 08:23:39
在C中的起始索引是0不是1,因此你應該從i = 0開始 – user411313 2014-12-05 08:53:41