2014-12-05 95 views
1

我有一個我正在使用的程序(安靜簡單),需要使用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); 
} 
+0

什麼是你的輸入格式? – timrau 2014-12-05 08:23:39

+0

在C中的起始索引是0不是1,因此你應該從i = 0開始 – user411313 2014-12-05 08:53:41

回答

1

你必須開始與i = 0而不是i = 1讓你的數組的第一個元素。你應該用i < n替換i <= n

for(i = 0; i < n; i++) 
{ 
    ... 
} 
+0

感謝你看這個,並且令人驚訝地解決了它。你能解釋爲什麼那樣嗎?爲什麼與我以前的方法不同呢?不管數組的位置如何,ID [i] .Middle_Name都不應該有值嗎? – RenegadeJ 2014-12-05 08:33:06

+0

@RenegadeJ數組的第一個元素位於索引0處。通過編寫'ID [1] .Middle_Name',您正在查找數組第二個元素的中間名。如果在數組中只有一個元素進行了測試,則ID [1]不存在,並且應該導致出現界限錯誤。換句話說,如果你創建一個包含10個元素的數組,最後一個元素將位於索引9,並且位於索引0的第一個元素。 – Aleph0 2014-12-05 08:46:59

0

我會建議更換的scanf( 「%S」,ID [1]。...)以獲取 如。

gets(ID[i].Middle_Name); 

而且也從0在環路代替1-

爲(I = 0;我< N; i ++在)開始 { ..... }

+0

感謝您的提示,將會做到。另外,從0開始實際上修復了它。你碰巧知道這是爲什麼?我的意思是,只要你解決相同/正確的數組索引,它在哪裏開始都沒有關係,對吧? – RenegadeJ 2014-12-05 08:43:26

+0

你說得對,如果你不小心按了姓氏的空間,那麼這也可能導致了這種情況。 – 2014-12-05 08:48:07

-1

有一個在你的代碼錯誤的語法

struct Students ID[n]; 

數組長度必須是一個常數,也可以使用新的這樣

struct Students *ID = new struct Students[n]; 
+1

ID [n]是正確的,是VLA。你的「新」是C++而不是C. – user411313 2014-12-05 08:50:37

+0

@ user411313好的,我得到了,謝謝 – Robin 2014-12-05 10:43:18

0
for(i = 0; i <= n; i++) 
{ 

//Your input code 

} 

for(i = 0; i <= n; i++) 
{ 

     // your output code 

} 
+0

如果你保持這種狀態,你將循環太多次。你必須用'i Aleph0 2014-12-05 11:59:58

+0

是的,你是對的。 謝謝 – 2014-12-05 14:13:37

+0

雖然這段代碼可能回答這個問題,但提供關於如何解決問題和/或爲什麼解決問題的附加上下文會提高答案的長期價值。 – 2017-03-17 08:34:47

-1
// <-- note leading ' ' on all format strings 
// <-- always check returned value from input statements 
// <-- following line would try to access past end of array, 
//  arrays start with offset of 0, 
//  for(i = 1; i <= n; i++) 

#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: "); 
    if(1 != scanf(" %d", &n)) 
    { 
     perror("scanf failed reading num students"); 
     exit(EXIT_FAILURE); 
    } 

    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\n"); 


    for(i=0; i< n; i++) 
    { 
     printf("Please enter the first name of student with ID: %d \t", i+1); 
     if(1 != scanf(" %s", (ID[i].First_Name))) 
     { 
      perror("scanf failed for first name"); 
      exit(EXIT_FAILURE); 
     } 

     printf("\n"); 

     printf("Please enter the last name of student with ID: %d \t", i+1); 
     if(1 != scanf(" %s", (ID[i].Last_Name))) // <-- check returned value 
     { 
      perror("scanf failed for last name"); 
      exit(EXIT_FAILURE); 
     } 

     printf("\n"); 

     printf("Please enter the middle name of student ID: %d \t", i+1); 
     if(1 != scanf(" %s", (ID[i].Middle_Name))) // <-- check returned value 
     { 
      perror("scanf failed for middle name"); 
      exit(EXIT_FAILURE); 
     } 

     printf("\n"); 

     printf("Please enter the age of student ID: %d \t", i+1); 
     if(1 != scanf(" %d", &(ID[i].Age))) // <-- check returned value 
     { 
      perror("scanf failed for student age"); 
      exit(EXIT_FAILURE); 
     } 

     printf("\n"); 
    } // end while 

    printf("In your class, we have: \n"); 
    printf("%s", ID[1].Middle_Name); 

    // <-- following line would try to access past end of array, arrays start with offset of 0, not 1 
    //for(i = 1; i <= n; i++) 
    for(i=0; 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); 
     printf("\n"); // put each student on a new line 
    } // end for 
    return(0); 
}