在下面的代码中是否有其他方法可以检索字符串数据? [英] Is there an alternative method to retrieve the string data in the code below?

查看:40
本文介绍了在下面的代码中是否有其他方法可以检索字符串数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,要求用户输入有关新生婴儿的各种详细信息.由于某种原因,该程序跳过了输入父母姓名的提示.它们可以工作.是否有任何方法可以修改代码以允许进行此输入?

In the code below the user is asked to input various details about a newborn child.For some reason the program skips over the prompt to enter the parents' names.I have tried using the scanf,gets and fgets functions but none of them work.Is there any a way to modify the code to allow this input to be made?

这是代码:

#include<stdio.h>
#include<string.h>

typedef struct
{

    char name[50];
    float weight;
    int year;
    char parent_names[50];

}newborn;


int main()
{
    
    int i;
    newborn child[2];
    for(i=0;i<2;i++)
    {
        printf("\nWhat is the child's name?\n");
        fgets(child[i].name,50,stdin);
        
        
        printf("What is the child's weight in Kg?\n");
        scanf("%f",&child[i].weight);
        
        
        printf("What is the child's year of birth?\n");
        scanf("%d",&child[i].year);
        
        
        printf("What are the names of the Mother and Father?\n");
        fgets(child[i].parent_names,50,stdin);  

        printf("Name:%s\n",child[i].name);
        printf("Weight:%f\n",child[i].weight);
        printf("Year of Birth:%d\n",child[i].year);
        printf("Parent names:%s\n",child[i].parent_names);
            
    }
}

推荐答案

这是因为此 scanf(%d",& child [i] .year); 捕获了int但离开了 \ n 作为要从 stdin 中读取的下一个字符. fgets 将流读取到 \ n ,因此对 fgets 的调用将捕获该 \ n 并继续.它做得很好,只是您忘记了stdin流上的 \ n .要修复该问题,请创建一个字符串,并以字符串形式捕获重量和年份的输入,然后使用 atof 将它们转换为 float int 来自 stdlib.h 的atoi ,如下所示:

It's because this scanf("%d",&child[i].year); captures the int but leaves the \n as the next character to be read from stdin. fgets reads stream to the \n, so the call to fgets captures that \n and continues. It's doing everything properly, except that you forgot about the \n on the stdin stream. To fix that create a string and capture the input for weight and year as strings and convert them to float and int using atof and atoi from stdlib.h, like this:

int i;
char buf[10];
newborn child[2];
for(i=0;i<2;i++)
{
    printf("\nWhat is the child's name?\n");
    fgets(child[i].name,50,stdin);
    
    
    printf("What is the child's weight in Kg?\n");
    fgets(buf, 10, stdin);
    child[i].weight = atof(buf);
    
    
    printf("What is the child's year of birth?\n");
    fgets(buf, 10, stdin);
    child[i].year = atoi(buf);
    
    
    printf("What are the names of the Mother and Father?\n");
    fgets(child[i].parent_names,50,stdin);  

    printf("Name:%s\n",child[i].name);
    printf("Weight:%f\n",child[i].weight);
    printf("Year of Birth:%d\n",child[i].year);
    printf("Parent names:%s\n",child[i].parent_names);
        
}

注意:您不能像使用 stdout 流那样使用 fflush 刷新 stdin 流,因此您始终必须请记住在执行操作时调用 scanf 后留在stdin中的 \ n .

NOTE: you can't use fflush to flush the stdin stream as you can with the stdout stream, so you always have to be mindful of the \n that is left in stdin after a call to scanf as you were doing.

这篇关于在下面的代码中是否有其他方法可以检索字符串数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆