首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >scanf和get buffer

scanf和get buffer
EN

Stack Overflow用户
提问于 2011-07-16 03:41:58
回答 4查看 540关注 0票数 1

我在使用scanf和get时遇到了问题。我知道这必然会出错,但我找不到任何其他方法。这样,可以打印出名字,但不会打印出名字的第一个字母。下面是我的代码:

代码语言:javascript
复制
#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*/

示例输出:

代码语言:javascript
复制
---------------------------------------------------------
| (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 ==========
EN

回答 4

Stack Overflow用户

发布于 2011-07-16 04:12:02

您的while循环和gets()的用法是generally not good practice

尝试如下所示:

代码语言:javascript
复制
 fflush(stdin);
 fgets(pRecords[counter]->name, sizeof(pRecords[counter]->name), stdin);

试一试

代码语言:javascript
复制
     if (strlen(pRecords[counter]->name) > 0)
     {
         pRecords[counter]->name[strlen(pRecords[counter]->name) - 1] = '\0';
     }
票数 1
EN

Stack Overflow用户

发布于 2011-07-16 03:53:07

您将第一个字符输给了while(getchar()=='\n');。我不知道为什么这个语句是必要的,但它会循环,直到它得到一个不是'\n‘的字符(在您的例子中是'j’和'c‘)。

票数 0
EN

Stack Overflow用户

发布于 2011-07-16 03:54:09

代码语言:javascript
复制
while (getchar() == '\n');

这一行包含换行符加上一个字符。当getchar()没有返回换行符时,它已经使用了第一个字符。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6712338

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档