扫描并获取缓冲区



im在scanf和gets方面遇到问题。我知道肯定会出错,但我找不到其他方法。通过这种方式,名称会打印出来,但不会打印出它的第一个字母。这是我的代码:

#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 recordt(D)isplay dbt(U)pdate recordt|n");
      printf("|t(L)oad diskt(W)rite diskt(E)mpty diskt|n");
      printf("|t(S)ort dbt(C)lear dbt(Q)uit tt|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:%.1fn",      pRecords[counter]->name,pRecords[counter]->age,pRecords[counter]->highBP,pRecords[counter]->lowBP); 
     printf("risk factor: %.1fn", 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: %sn", pRecords[i]->name); 
              printf("age: %dn", pRecords[i]->age);
              printf("bp: %.2f %.2fn", pRecords[i]->highBP, pRecords[i]->lowBP);
              printf("risk: %.2fnn", 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 ==========

while循环和使用gets()通常不是一种好的做法。

试试类似的东西:

 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] = '';
     }

您将丢失while(getchar()=='n');的第一个字符。我不知道为什么这个语句是必要的,但它会循环,直到它得到一个不是'\n'的字符(在您的情况下是'j'和'c'(。

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

这一行吃掉换行符加上一个字符。当getchar((不返回换行符时,它已经消耗了第一个字符。

查看ungetc((将该字符写回到流中。

这:

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

循环,直到它得到一个非换行符,这将是名称的第一个字符。

试试这个:

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

最新更新