【问题标题】:scanf issue multiple inputscanf 发出多个输入
【发布时间】:2017-07-16 13:27:17
【问题描述】:

每当我尝试扫描我的输入时,例如:JASON BOURNE JULY 5 1972,程序就会崩溃。

scanf("%s %s %s %d %d", temp->fname, temp->lname, temp->month, temp->day, temp->year);

我确定它与%d's 而如果我输入:

scanf("%s %s %s", temp->fname, temp->lname, temp->month);
scanf("%d %d", temp->month, temp->day);

String 的值是正确的,程序在 int 被分配之前就崩溃了。

这是我的结构的副本:

typedef struct node{
    char fname [29];
    char lname [29];

    char month [9];
    int day;
    int year;

    struct student * next;
    struct student * previous;
} student;

这是我在 main() 中的函数的副本:

    student * head = malloc(sizeof(student));
    student * temp = head;
    int num = numStudents;
    while(num != 0){
        scanf("%s %s %s %d %d", temp->fname, temp->lname, temp->month, temp->day, temp->year);
        printf("FUNCTION NEVER REACHES THIS POINT");
        temp = temp->next = malloc(sizeof(student));
        num--;
    }temp->next = NULL;

【问题讨论】:

  • scanf%d 的期望是什么类型的参数?
  • 将 & 与变量一起用于 ℅d 说明符。
  • @KyleBatchelor 不,不应该。为什么你认为它应该期望一个整数?
  • 如果每个人都使用 gcc -Wall -Werror,那么 50% 标记为 c 的 Stack Overflow 问题将永远不会被问到。 "scanf 应该期望一个整数值" 是完全错误的。 scanf 是如何赋值的? 4 传递给它?
  • 你的论点根本不是指向temp 是值,而不是指针。

标签: c struct int scanf


【解决方案1】:

如果有人感兴趣,我会这样做以使一切正常工作:

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

//Struct for Student
struct student{
    char fname [29];
    char lname [29];

    char month [9];
    int day;
    int year;

    struct student *next;
    struct student *previous;
};

//Pre-Call
void printList(struct student *node);
struct student * insertNode(struct student * list, char fname[29], char lname[29], char month[9], int day, int year);


//Main function
int main(){
    //Take Input and Assign Variables
    int classes, i;
    scanf("%d", &classes);

    for(i = 0; i < classes; i++){
        int numStudents, j;
        scanf("%d", &numStudents);

        struct student *list = NULL; //Create LL
        int num = numStudents, day, year;
        char fname[29]; char lname[29]; char month[9];
        //Assign Values
        while(num != 0){
            scanf("%s %s %s %d %d", &fname, &lname, &month, &day, &year);
            list = insertNode(list, fname, lname, month, day, year);
            num--;
        }

        //

        printList(list);
    }


    return 0;
}


//Function to insert a node to the LL
struct student * insertNode(struct student * list, char fname[29], char lname[29], char month[9], int day, int year){
    //If the list is empty
    if(list == NULL){
        struct student * tempNode = (struct student *) malloc(sizeof(struct student));
        strcpy(tempNode->fname,fname);
        strcpy(tempNode->lname,lname);
        strcpy(tempNode->month,month);
        tempNode->day = day;
        tempNode->year = year;
        tempNode->next = NULL; // Extremely Important
        return tempNode;
    }
    //If the list has a node
    list->next = insertNode(list->next, fname, lname, month, day, year);
    return list;
}


//Function to print a LL
void printList(struct student * node){
    if(node->next == NULL)
        printf("%s %s %s %d %d\n", node->fname, node->lname, node->month, node->day, node->year);
    else
    {
        printf("%s %s %s %d %d\n", node->fname, node->lname, node->month, node->day, node->year);
        printList(node->next);
    }
}

【讨论】:

  • 你应该在调用insertNode()之前检查来自scanf()的返回值;您不想从新行和旧行插入不完整的数据混搭,或者在遇到 EOF 时重复上一个条目。始终检查输入函数——它们(通常)失败,并且(通常)遇到 EOF。即使这看起来很痛苦,也要去做——这是必要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-04
  • 2015-12-15
  • 2019-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-13
相关资源
最近更新 更多