2011-07-15 39 views
1

即時通訊有問題的scanf和獲取。我知道它的錯誤,但我找不到任何其他方式。這樣,該名稱正在打印出來,但它不打印出它的第一個字母。 這裏是我的代碼:scanf並獲取緩衝區

#include <stdio.h> 
float calculations(int age, float highBP, float lowBP); 
char option; 
int counter, age; 
char temp_name[50]; 
float highBP, lowBP, riskF, optimalH = 120.0, optimalL = 80.0; 


typedef struct { 

    char name[50]; /*which represents the patient’s name*/ 
    int age;  /*which represents the patient’s age*/ 
    float highBP; /*highBP, which represents the patient’s high (systolic) blood pressure*/ 
    float lowBP; /*lowBP, which represents the patient’s low (diastolic) blood pressure*/ 
    float riskF; /*riskFactor, which represents the patient’s risk factor for stroke due to hypertension.*/ 
}patient;/*end structure patient*/ 


patient *pRecords[30]; 

void printMenu() 
{ 

     printf("\n---------------------------------------------------------\n"); 
     printf("|\t(N)ew record\t(D)isplay db\t(U)pdate record\t|\n"); 
     printf("|\t(L)oad disk\t(W)rite disk\t(E)mpty disk\t|\n"); 
     printf("|\t(S)ort db\t(C)lear db\t(Q)uit \t\t|\n"); 
     printf("---------------------------------------------------------\n"); 
     printf("choose one:"); 



    }/*end print menu*/ 

void enter() 
{ 

    if(counter == 30) 
     printf("database full."); 
    else{ 

    printf("name: "); 
    while(getchar()=='\n'); 
    gets(temp_name); 
    strcpy(pRecords[counter]->name , temp_name); 
    printf("age: ");  scanf("%d", &age); 
    pRecords[counter]->age = age; 
    printf("highBP: "); scanf("%f", &highBP); 
    pRecords[counter]->highBP = highBP; 
    printf("lowBP: "); scanf("%f", &lowBP); 
    pRecords[counter]->lowBP = lowBP;  
    float temp = calculations(age, highBP,lowBP); 
    pRecords[counter]->riskF = temp; 
    /*printf("name: %s, age: %d, highbp:%.1f, lowBP:%.1f\n",  pRecords[counter]->name,pRecords[counter]->age,pRecords[counter]->highBP,pRecords[counter]->lowBP); 
    printf("risk factor: %.1f\n", pRecords[counter]->riskF);*/ 
    counter ++; 
    } 
}/*end of void enter function*/ 

memallocate(int counter){ 
       pRecords[counter] = (patient *)malloc (sizeof(patient)); 
}/*end memallocate function*/ 


void display() 
{ 
    printf("===============================\n"); 
    int i; 
    for(i=0; i<counter; i++) 
    { 
       printf("name: %s\n", pRecords[i]->name); 
       printf("age: %d\n", pRecords[i]->age); 
       printf("bp: %.2f %.2f\n", pRecords[i]->highBP, pRecords[i]->lowBP); 
       printf("risk: %.2f\n\n", pRecords[i]->riskF);    
    }/*end of for loop*/ 
    printf("========== %d records ==========", counter); 
    }/*end of display method*/ 

float calculations(int age, float highBP, float lowBP) 
{ float risk; 
    if((highBP <= optimalH) && (lowBP <= optimalL)) 
     { risk = 0.0; 
     if(age >=50) 
       risk = 0.5; 
     } 
    else if(highBP <= optimalH && (lowBP>optimalL && lowBP <=(optimalL+10))) 
    {  risk= 1.0; 
      if(age >=50) 
       risk = 1.5; 
    } 
    else if ((highBP >optimalH && highBP <= (optimalH+10))&& lowBP <=optimalL) 
    {  risk= 1.0; 
      if(age >=50) 
       risk= 1.5; 
    } 
    else if((highBP > optimalH && highBP <=(optimalH+10)) && (lowBP >optimalL && lowBP <= (optimalL+10))) 
    {  risk= 2.0; 
      if(age >=50) 
       risk = 2.5; 
    } 
    else if(highBP < optimalH && (lowBP >(optimalL+11) && lowBP<(optimalL+20))) 
    {  risk = 3.0; 
      if(age >=50) 
       risk = 3.5; 
    } 
    else if((lowBP < optimalL) && (highBP >(optimalH+11) && highBP<(optimalH+20))) 
    {  risk = 3.0; 
      if(age >=50) 
       risk = 3.5; 
    } 
    else if((highBP>=(optimalH+11) && highBP <= (optimalH+20))&& (lowBP>=(optimalL+11) && lowBP<=(optimalL+20))) 
    {  risk = 4.0; 
      if(age >=50) 
       risk = 4.5; 
    } 
    else 
    {  risk = 5.0; 
      if(age >=50) 
       risk = 5.5; 
    } 
    return risk; 

}/*end of calculation function*/ 

main() 
{ 

     printMenu(); 
     char option=getchar(); 
while(option != 'q' || option != 'Q'){ 
     if(option == 'N' || option == 'n') 
     { 
       memallocate(counter); 
       enter(); 
       printMenu(); 
     } 
     if (option == 'L' || option == 'l') 
     { 

      printMenu(); 
     } 
     if(option == 'S' || option == 's') 
     { 

      printMenu(); 
      } 
     if(option == 'D' || option == 'd') 
     { 
      display(); 
      printMenu(); 
      } 
     if(option == 'W' || option == 'w') 
     { 

      printMenu(); 
      } 
     if(option == 'C' || option == 'c') 
     { 

      printMenu(); 
      } 
     if(option == 'U' || option == 'u') 
     { 

      printMenu(); 
      } 
     if(option == 'E' || option == 'e') 
     { 

      printMenu(); 
      } 
     if(option == 'Q' || option == 'q') 
     { 
      exit(0); 

      } 

     option = getchar(); 

     }/*end while*/ 
     system("pause"); 

}/*end main*/ 

輸出樣本:

--------------------------------------------------------- 
| (N)ew record (D)isplay db (U)pdate record | 
| (L)oad disk (W)rite disk (E)mpty disk | 
| (S)ort db (C)lear db (Q)uit | 
--------------------------------------------------------- 
choose one: n 
name: judy 
age: 30 
high bp: 110 
low bp: 88 
3 
--------------------------------------------------------- 
| (N)ew record (D)isplay db (U)pdate record | 
| (L)oad disk (W)rite disk (E)mpty disk | 
| (S)ort db (C)lear db (Q)uit | 
--------------------------------------------------------- 
choose one: n 
name: cindy white 
age: 52 
high bp: 100.7 
low bp: 89.4 
--------------------------------------------------------- 
| (N)ew record (D)isplay db (U)pdate record | 
| (L)oad disk (W)rite disk (E)mpty disk | 
| (S)ort db (C)lear db (Q)uit | 
--------------------------------------------------------- 
choose one: d 
=============================== 
name: udy 
age: 30 
bp: 110.00 88.00 
risk: 1.0 

name: indy white 
age: 52 
bp: 100.70 89.40 
risk: 1.5 
========== 2 records ========== 
+1

請歸結示例代碼的測試用例,可以說明你的問題。 – jbruni

回答

0

你失去了第一個字符while(getchar()=='\n');。我不知道爲什麼這個陳述是必要的,但它循環直到它得到一個不是'\ n'的字符(在你的案例中是'j'和'c')。

+0

因爲如果我把它拿出來,它不接受用戶輸入的名字。它跳過了年齡。 – Nidale

+0

@Nidale嗯......有趣。我建議你嘗試'fflush(stdin);'在接受輸入名稱之前刷新輸入流中的剩餘字符。 – Ram

+0

是的,我只是.. ..它仍然不工作的原因。 – Nidale

0
while (getchar() == '\n'); 

此行消耗換行符加一個字符。當getchar()不返回換行符時,它已經消耗了第一個字符。

查看ungetc()將該字符寫回到流中。

0

此:

while(getchar()=='\n'); 

循環,直到它得到一個非換行,這將是名稱的第一個字符。

試試這個:

do 
    c = getchar(); 
while(c == '\n'); 
ungetc(c, stdin); 
+0

現在,它只是不讓我接受用戶輸入。 – Nidale

+0

您可以將'printf(「name:」);'行的內容複製並粘貼到'printf(「age:」);'。 – MRAB

1

while循環和使用gets()generally not good practice

試着這麼做:

fflush(stdin); 
fgets(pRecords[counter]->name, sizeof(pRecords[counter]->name), stdin); 

嘗試

 if (strlen(pRecords[counter]->name) > 0) 
    { 
     pRecords[counter]->name[strlen(pRecords[counter]->name) - 1] = '\0'; 
    } 
+0

謝謝你,先生!它完美的作品! – Nidale

+0

不客氣! –

+0

kk我知道我說它的工作完美..但現在,我遇到了問題..它在字符串的末尾存儲新的行字符,所以每次我打印它,它會打印一個新行 – Nidale