current position:Home>Unable to realize the output after inputting the overdue book information linked list. I don't know where the input and output problems occur and how to solve them?

Unable to realize the output after inputting the overdue book information linked list. I don't know where the input and output problems occur and how to solve them?

2022-01-26 22:30:21 CSDN Q & A

The phenomenon and background of the problem

The output is :

img

#pragma warning(disable:4996) // Ban Vusial Studio Data overflow error prompt for #include <iostream>#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <string.h>#include <string.h>#include <math.h>#define LOSCXX sizeof(struct scxx) // The length of book overdue information entered for the first time struct scxx   // The book overdue information entered for the first time {    char grxx[30];  // personal     char bjxx[30];  // class     char tsxx[30];  // Book information     int cqts; // How many days are overdue     int fkje; // The amount of the fine is     struct scxx *next;};int n;struct scxx*creatsc(void) // Functions that return pointers {    struct scxx *head;    struct scxx *ps1, *ps2;    n = 0;    ps1 = ps2 = (struct scxx*)malloc(LOSCXX);    scanf("%s,%s,%s,%d,%d", &ps1->grxx, &ps1->bjxx, &ps1->tsxx, &ps1->cqts, &ps1->fkje);    head = NULL;    while(strcmp(ps1->grxx,"end") != 0)    {        n = n + 1;        if (n == 1)head = ps1;        else ps2->next = ps1;        ps2 = ps1;        ps1 = (struct scxx*)malloc(LOSCXX);        scanf("%s,%s,%s,%d,%d", &ps1->grxx, &ps1->bjxx, &ps1->tsxx, &ps1->cqts, &ps1->fkje);    }    ps2->next = NULL;    return(head);}void print(struct scxx*head){    struct scxx* p;    p = head;    if(head!=NULL)        do { printf("%s,%s,%s,%d,%d", p->grxx, p->bjxx, p->tsxx, p->cqts, p->fkje);    p = p->next;    } while (p != NULL);}int main(){    struct scxx *head;    head = creatsc();    print(head);    return 0;}
Operation results and error reporting contents

![img](
Change the input / output type

What I want to achieve

Realize the input and output of overdue book linked list https://img-mid.csdnimg.cn/release/static/image/mid/ask/49813533693617.png "#left")




Refer to the answer 1:

1、 hold scanf("%s,%s,%s,%d,%d", &ps1->grxx, &ps1->bjxx, &ps1->tsxx, &ps1->cqts, &ps1->fkje); Change it to
scanf("%s",&ps1->grxx);
scanf("%s",&ps1->bjxx);
scanf("%s",&ps1->tsxx);
scanf("%d",&ps1->cqts);
scanf("%d",&ps1->fkje);
2、 I guess you're just beginning to learn C Language , debugged , Can see , If you assigned the value like that before , All strings are assigned to ps1->grxx.
3、 The following figure shows me debugging with your code , Input string view ps1 Value .

img




Refer to the answer 2:

copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2022/01/202201262230187152.html

Random recommended