开发者

scanf and gets buffer

开发者 https://www.devze.com 2023-03-20 20:48 出处:网络
im having a problem with scanf and gets. and I kno that its bound to errors but I couldn\'t find any other way. This way, the name is printing out but It doesn\'t print out the first letter of it.

im having a problem with scanf and gets. and I kno that its bound to errors but I couldn't find any other way. This way, the name is printing out but It doesn't print out the first letter of it. Here's my code:

#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 =开发者_StackOverflow= 'E' || option == 'e')
      {

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

           }

      option = getchar(); 

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

}/*end main*/

sample output:

---------------------------------------------------------
| (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 ==========


Your while loop and use of gets() is generally not good practice.

Try something like:

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

Try

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


You lose the first character to while(getchar()=='\n');. I don't know why that statement is necessary, but it loops until it gets a character that is not '\n' (which is 'j' and 'c' in your case).


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

This line eats the newlines plus one character. When getchar() does not return a newline, it has already consumed the first character.

Look at ungetc() to write that character back onto the stream.


This:

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

loops until it gets a non-newline, which will be the first character of the name.

Try this instead:

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

精彩评论

暂无评论...
验证码 换一张
取 消