2015-04-29 42 views
2

我有一個問題與我的代碼,我改變了一些功能,以適應我添加的結構,所以而不是有變量遍佈整個地方,但現在它根本不工作。我需要它來創建一個結構人,然後提示用戶輸入人名和年齡;那麼它會要求更多的人填寫一個雙向鏈表,如果沒有輸入任何人名,就會停止循環。然後它將我輸入到雙向鏈表中的內容反過來。所有的幫助表示讚賞^ -^寫入字符串結構中的雙向鏈表C

struct person 
{ 
    char name[10][41]; 
    int age[10]; 
}; 

int write(struct person *people); 
void print(struct person *people); 
int main(void) 
{ 
    char names[10][41]; 
    int n = 10; 
    int ages[10]; 

    typedef struct person people; 

    n = write(people); 
    print(people); 


    system("PAUSE"); 
    return 0; 
} 


int write(struct person *people) 
{ 
    int i; 
    char name[41]; 
    int age[10]; 
    for(i=0; i<=i; i++) 
    { 
     fflush(stdin); 
     printf("Enter full name\n"); 

     gets(people.name); 
     strcpy(names[i], name); 

     if(names[i][0] == '\0') 
      break; 

     printf("Enter their age\n"); 
     scanf("%d", &age[i]); 
     ages[i] = age[i]; 
    } 
} 

void print(struct person *people) 
{ 
    int i = 0; 
    for(i = 0; i < 10; i++) 
    { 
     if(names[i][0] == '\0') 
      break; 

     printf("%s is %d year(s) old\n", names[i], ages[i]); 
    } 
    return i; 
} 
+1

這是什麼:'爲(i = 0;我<= I; i ++)'? – wildplasser

+0

正在使用它來測試我的循環,直到我知道它工作之前不想改變它 – badcoderiter

回答

0
  1. 你傳入您剛纔定義的聲明類型的變量,而不是一個類型的名稱,這

    typedef struct person people; 
    

    應該

    struct person people; 
    
  2. 更改

    n = write(people); 
    

    n = write(&people); 
    
  3. 刪除fflush(stdin)這是不確定的行爲。

  4. 不要使用gets()這是非常不安全的,使用fgets()代替

    char name[40]; 
    gets(name); 
    

    應該

    char name[40]; 
    fgets(name, sizeof(name), stdin);