Scanf总是跳过第二行输入 [英] Scanf skips second line of input, always

查看:61
本文介绍了Scanf总是跳过第二行输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取文本数据行,并将其存储在链接列表中.输入数据如下所示:

I'm trying to read lines of text data, and store them in a linked list. Input data looks something like this:

David Copperfield
Charles Dickens
4250
24.95
32.95
10
6
END_DATA

我读取数据的方式是先读取第一行,看看它是否为END_DATA.如果不是,那么我将第一行传递到一个函数中,该函数创建一个链接列表节点,其中包含书籍数据.由于某种原因,在我将第一行传递给函数之后,scanf不会读取第二行.

The way I read the data is to first read the first line to see if it is END_DATA. If it's not, then I pass the first line into a function which creates a linked list Node with the book data inside. For some reason, scanf does not read the second line after I pass the first line to the function.

当节点读取数据后尝试打印它们时,我的输出看起来像这样.

When I try to print the Nodes after they've read the data, my output looks like this.

David Copperfield 

0 
0.000000 
0.000000 
0 
0
Charles Dickens 

4250 
24.950001 
32.950001 
10 
6
Charles Dickens 

0 
0.000000 
0.000000 
0 
0

源代码在下面

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


struct NodeRec {
    Book data;
    struct NodeRec *next;
};

typedef struct NodeRec Node;

float findRevenue(Node *head);
float totalWholesaleCost(Node *head);
float totalProfit(Node *head);
float totalBooksSold(Node *head);
float averageProfitPerSale(Node *head);
void printNodes(Node *head);

void insertNode(Node* head, Node* newNode);

Node* createNewNode(char titleS[40]) {
    Node* newNode;
    newNode = malloc(sizeof(Node));
    if (newNode == NULL){
        printf("ERROR ALLOCATING SPACE");
        exit(-1);
    }
    else {
        strcpy(newNode->data.title, titleS);
        scanf("%40[^\n]*c", (char *)&newNode->data.author);

        scanf ("%i %f %f %i %i", &newNode->data.number, &newNode->data.wholesaleprice, &newNode->data.retailprice, &newNode->data.wholesalequantity, &newNode->data.retailquantity);

        printf ("%s \n%s \n%i \n%f \n%f \n%i \n%i\n", newNode->data.title, newNode->data.author,newNode->data.number, newNode->data.wholesaleprice, newNode->data.retailprice, newNode->data.wholesalequantity, newNode->data.retailquantity);

    }

    return newNode;
}


void readBooks(Node* head) {

    char firstline[40];
    char enddataString[40] = "END_DATA";
    scanf("%40[^\n]*c", (char *)&firstline);


    while (strcmp(firstline, enddataString)) {

        Node* newNode = createNewNode(firstline);
        insertNode(head, newNode);

        scanf("%40[^\n]*c", (char *)&firstline);
    }
}



void insertNode(Node* head, Node* newNode) {

    if (head == NULL) {
        head = newNode;
    }
    else {
        Node *preNode, *currentNode;

        int n = newNode->data.number;
        currentNode = head;

        while (currentNode != NULL && (currentNode->data.number) > n ) {
            preNode = currentNode;
            currentNode = currentNode->next;

        }
        newNode->next = currentNode;
        preNode->next = newNode;
    }
}

推荐答案

主要问题是先前输入%i%f%f%i剩下的'\ n'%i"留在 stdin 中.当执行 scanf(%40 [^ \ n] ... "时,将不保存任何内容,并且'\ n'仍保留在 stdin 中.

The main issue is that a leftover '\n' from previous input "%i %f %f %i %i" is left in stdin. When scanf("%40[^\n]... is executed, nothing is save and '\n' remains in stdin.

故事的道德感

  1. 始终检查I/O功能的结果,例如 fopen,fseek,fscanf,scanf,fgets,fgetc,...

请勿使用 scanf()

使用 fgets() getline()获取一行数据.然后解析它,并在进行过程中检查意外结果. SO 上的代码更多,但时间更少.

Use fgets() or getline() to get a line of data. Then parse it, checking for unexpected results as you go. A bit more code, but less time on SO.

这篇关于Scanf总是跳过第二行输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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