链表用C用于fgets() [英] Linked list in C with fgets()

查看:116
本文介绍了链表用C用于fgets()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的code跳过第二数据结构中的第一个问题的问题。我是pretty确保它是因为得到(),但不能肯定。我想,我试图与fgets(),但它仍然是给我的问题。为什么呢?

 的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;#定义NumberOfActresses 5typedef结构女主角
{
字符*姓名,* placeofbirth,*染发;
INT年龄;
浮networth;
结构女主角*接下来的;}女主角;无效PopulateStruct(女主角*节点)
{
与于节点GT;名=(的char *)malloc的(的sizeof(char)的* 50);
与于节点GT; placeofbirth =(字符*)malloc的(的sizeof(char)的* 50);
与于节点GT;染发=(字符*)malloc的(的sizeof(char)的* 50);的printf(请输入女星/演员的名字:);
得到(与于节点GT;名);
的printf(请输入演员/演员出生地:);
得到(与于节点GT; placeofbirth);
的printf(请输入女星/演员发色:);
得到(与于节点GT;染发);
的printf(请输入演员/演员年龄:);
scanf函数(%d个,&安培;&于节点GT;年龄);
的printf(请输入演员/演员networth:);
scanf函数(%F,&安培;&于节点GT; networth);
}无效DisplayStruct(女主角*头)
{
女主角*履带;履带=头;而(履带!= NULL)
{
    的printf(女演员/演员的名字是:%S \\ n,crawler->名);
    的printf(出生的女演员/演员的地方是:%S \\ n,crawler-> placeofbirth);
    的printf(女演员/演员的头发颜色是:%S \\ n,crawler->染发);
    的printf(女演员/演员年龄数:%d \\ n,crawler->年龄);
    的printf(为女演员/演员networth是%F \\ N,crawler-> networth);
    履带= crawler->接下来,
}
}诠释的main()
{
INT I;
女主角*头=(女主角*)malloc的(的sizeof(女主角)),*履带;履带=头; 对于(i = 0; I< NumberOfActresses;我++)
    {
    PopulateStruct(履带);
    如果(ⅰ== 2)
        crawler->接着= NULL;
    其他
        crawler->接下来=的malloc(sizeof的(演员));    履带= crawler->接下来,
}履带= NULL;DisplayStruct(头);返回0;
}


解决方案

混合与fgets scanf函数总是证明厉害。其原因是, scanf函数将离开换行字符输入流中,下面与fgets 因此将读取空线。并使用获得 完全错误。

解决的办法是经常阅读与与fgets 输入,然后解析与的sscanf 输入需要。对于其中的sscanf 是不需要的情况下,即输入是一个字符串,可以使用 strcspn 来去除新行从缓冲

  INT PopulateStruct(女主角*节点)
{
    炭缓冲器[100];    的printf(请输入女星/演员的名字:);
    如果(与fgets(缓冲区,缓冲区的sizeof,标准输入)== NULL)
        返回0;
    缓冲[strcspn(缓冲区,\\ n)] ='\\ 0';
    如果((&于节点GT;名称=的malloc(strlen的(缓冲)+ 1))== NULL)
        返回0;
    的strcpy(与于节点GT;名,缓冲区);    //同上的出生地和发色    的printf(请输入演员/演员年龄:);
    如果(与fgets(缓冲区,缓冲区的sizeof,标准输入)== NULL)
        返回0;
    如果(sscanf的(缓冲器,%d个,&放大器;于节点>!年龄)= 1)
        返回0;    的printf(请输入演员/演员networth:);
    如果(与fgets(缓冲区,缓冲区的sizeof,标准输入)== NULL)
        返回0;
    如果(sscanf的(缓冲器,%LF,&放大器;于节点>!networth)= 1)
        返回0;    返回1;
}

哦,我改变了 networth 浮动双击。 A 浮动只有6位或7位precision的,这是远远不够的女演员/演员的身家。

I'm having issues with my code skipping the first question in the second data structure. I'm pretty sure it's because the gets(), but not sure. I think I tried fgets(), but it still was giving me issues. Why?

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

#define NumberOfActresses 5

typedef struct Actress
{
char *name, *placeofbirth, *haircolor;
int age;
float networth;
struct Actress *next;

} Actress;

void PopulateStruct(Actress *node)
{
node->name = (char *) malloc(sizeof(char) * 50);
node->placeofbirth = (char *) malloc(sizeof(char) * 50);
node->haircolor = (char *) malloc(sizeof(char) * 50);

printf("Please enter the name of the actress/actor: ");
gets(node->name);
printf("Please enter the actress/actor place of birth: ");
gets(node->placeofbirth);
printf("Please enter the actress/actor hair color: ");
gets(node->haircolor);
printf("Please enter the actress/actor age: ");
scanf("%d", &node->age);
printf("Please enter the actress/actor networth: ");
scanf("%f", &node->networth);
}

void DisplayStruct(Actress *head)
{
Actress *crawler;

crawler = head;

while(crawler != NULL)
{
    printf("The name of the actress/actor is: %s\n", crawler->name);
    printf("The place of birth for the actress/actor is: %s\n",       crawler->placeofbirth);
    printf("The hair color of the actress/actor is: %s\n", crawler->haircolor);
    printf("The actress/actor age is: %d\n", crawler->age);
    printf("The networth for the actress/actor is %f\n", crawler->networth);
    crawler = crawler->next;
}
}

int main()
{
int i;
Actress *head = (Actress *) malloc(sizeof(Actress)), *crawler;

crawler = head;

 for (i = 0; i < NumberOfActresses; i++)
    {
    PopulateStruct(crawler);
    if (i == 2)
        crawler->next = NULL;
    else
        crawler->next = malloc(sizeof(Actress));

    crawler = crawler->next;
}

crawler = NULL;

DisplayStruct(head);

return 0;
}

解决方案

Mixing fgets and scanf always turns out badly. The reason is that scanf will leave the newline character in the input stream, and the following fgets will therefore read an empty line. And using gets is just plain wrong.

The solution is to always read the input with fgets, and then parse the input with sscanf as needed. For those cases where sscanf is not needed, i.e. the input is a string, you can use strcspn to remove the newline from the buffer.

int PopulateStruct(Actress *node)
{
    char buffer[100];

    printf("Please enter the name of the actress/actor: ");
    if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
        return 0;
    buffer[strcspn(buffer,"\n")] = '\0';
    if ( (node->name = malloc(strlen(buffer) + 1)) == NULL )
        return 0;
    strcpy( node->name, buffer );

    // ditto for place of birth and hair color

    printf("Please enter the actress/actor age: ");
    if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
        return 0;
    if ( sscanf( buffer, "%d", &node->age ) != 1 )
        return 0;

    printf("Please enter the actress/actor networth: ");
    if ( fgets( buffer, sizeof buffer, stdin ) == NULL )
        return 0;
    if ( sscanf( buffer, "%lf", &node->networth ) != 1 )
        return 0;

    return 1;
}

Oh, and I changed the networth from float to double. A float has only 6 or 7 digits of precision, and that's not nearly enough for the net worth of an actress/actor.

这篇关于链表用C用于fgets()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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