2016-01-24 107 views
1

我想編寫一個小程序來學習C;這裏是:scanf(「%[^ n]」)被跳過

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

int total; 
char suffix[3]; 
struct person { 
    char id[11]; 
    char name[21]; 
    char sex[7]; 
    int age; 
    char phone[12]; 
}; 

char* number_suffix (int number) { 
    int mod; 
    mod = number % 10; 

    switch (mod) { 
     case 1: 
      strcpy(suffix, "st"); 
      break; 
     case 2: 
      strcpy(suffix, "nd"); 
      break; 
     case 3: 
      strcpy(suffix, "rd"); 
      break; 
     default: 
      strcpy(suffix, "th"); 
      break; 
    } 
    return suffix; 
} 

void input_info (struct person info[], int total_people) { 
    int counter; 
    for (counter=0; counter<total_people; counter++){ 
     printf("%s%d%s%s\n","Please input the ID(10 digits) of ", (counter+1), 
       number_suffix(counter), " person: "); 
     scanf("%s", info[counter].id); 
     fflush(stdin); 

     printf("%s%d%s%s\n", "Please input the Name(20 chars) of ", (counter+1), 
       number_suffix(counter), " person: "); 
     scanf("%[^\n]", info[counter].name); 
     fflush(stdin); 

     printf("%s%d%s%s\n", "Please input the Sex(Male/Female) of ", (counter+1), 
       number_suffix(counter), " person: "); 
     scanf("%s", info[counter].sex); 
     fflush(stdin); 

     printf("%s%d%s%s\n", "Please input the Age(1~100) of ", (counter+1), 
       number_suffix(counter), " person: "); 
     scanf("%d", &info[counter].age); 
     fflush(stdin); 

     printf("%s%d%s%s\n", "Please input the Phone of ", (counter+1), 
       number_suffix(counter), " person: "); 
     scanf("%s", info[counter].phone); 
     fflush(stdin); 
    } 
    printf("%s\n%s\n%s\n%d\n%s\n", info[counter].id, info[counter].name, info[counter].sex, &info[counter].age, info[counter].phone); 
} 

int main (void) { 
    printf("%s\n", "Please input a number that how many people you want to record:"); 
    scanf("%d", &total); 
    fflush(stdin); 
    struct person *person_info = malloc(sizeof(struct person)*total); 
    input_info(person_info, total); 

    free(person_info); 
    return 0; 
} 

我發現了一些奇怪的東西,當我運行它。

Please input a number that how many people you want to record: 
1 
Please input the ID(10 digits) of 1th person: 
A01 
Please input the Name(20 chars) of 1th person: 
Please input the Sex(Male/Female) of 1th person: 
Male 
Please input the Age(1~100) of 1th person: 
32 
Please input the Phone of 1th person: 
1224464 
[empty line] 
[empty line] 
[empty line] 
1926234464 
[empty line] 

是這個程序在運行時跳過scanf("%[^\n]", info[counter].name);這行嗎?

爲什麼,它是什麼原因造成的?

+1

'fflush(標準輸入),不推薦'。在許多實現中,刷新輸入流是未定義的行爲。在你的特定情況下,它可能不會消除第一個'scanf'留下的換行符。請參閱[scanf被跳過](http://stackoverflow.com/questions/14484431/scanf-getting-skipped) – kaylum

+0

「_如何理解指針,struct,malloc,函數參數之間的關係?_」 - 什麼?這太寬泛了,無法回答。所以,我已經刪除它。 –

+0

'11st','12nd','13rd'? – Jasen

回答

1

fflush(stdin)根據C標準未定義,但它適用於某些實現。但最好避免它,因爲它不可移植並且可能會調用未定義的行爲。

要解決此問題,與

int c; /* Declare it once */ 
while((c = getchar()) != '\n' && c != EOF); /* Discards everything until a newline character or EOF */ 

的另一個問題是

printf("%s\n%s\n%s\n%d\n%s\n", info[counter].id, info[counter].name, info[counter].sex, &info[counter].age, info[counter].phone); 

全部更換fflush(stdin)原本應如此。

printf("%s\n%s\n%s\n%d\n%s\n", info[counter].id, info[counter].name, info[counter].sex, info[counter].age, info[counter].phone); 

,並應置於for循環。否則,你是因爲

  1. 你爲%d其預計的int通過了int*調用未定義的行爲。
  2. 您訪問超出分配的內存段的無效內存位置。

而且,正如其他人所說,通過counter + 1number_suffix

+0

你說有問題的'printf'行之間沒有區別。即使是你發佈的改變的也是一樣的。 –

+0

@AshishAhuja仔細看。我刪除了&符號。 –

+0

糟糕,我的錯誤:-( –

1
  1. 問題出在您的scanf模式。使用" %[^\n]",而不是"%[^\n]"不趕\n通(後以前的數據錄入)
  2. counter + 1number_suffix

如何理解指針,結構的malloc,功能參數之間的關係?

Understanding and Using C PointersO'Reilly Media公司

+2

點2錯誤請參見[Array Decay ](http://stackoverflow.com/a/1461449/3049655) –

+3

使用'%[^ \ n]'而不是'%s'的原因是爲了讓人們有兩個(或更多)空間(或空間):'John Doe'等 –

+0

爲什麼這本書的廣告?:-) –