2011-07-15 134 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