Fgets被忽略&程序只读取2个输入数据(链表)

时间:2014-04-13 11:39:03

标签: c struct fgets

  1. 当我运行它时,我的第一个fgets会被忽略,直接跳到第二个fgets为什么会这样?我希望fgets能够获得学生姓名的用户输入,但它并没有。所以我无法得到学生的名字

    printf("Enter the name of the student\n");
                fgets(name, 100, stdin);
    printf("Enter the gender of the student\n");
                fgets(gender,100,stdin);
    printf("Enter the course of the student\n");
                fgets(course,100,stdin);
                number++;
    
  2. 我的print_begin()函数可以输出2个输入数据,这意味着如果studentlist有4个结构,它只打印出2个。但是我的print_last可以很好地打印所有输入。

    void print_begin(){
    
    struct node *tempdisplay;
    tempdisplay = studentlist;
    
    if (studentlist == NULL)
    printf("List is empty\n");
    else
    {
        while (tempdisplay!= NULL)
        {
        printf("student name   = %s", tempdisplay->student_name);
        printf("student gender = %s", tempdisplay->student_gender);
        printf("student course = %s", tempdisplay->student_course);
        printf("Student Number = %d\n\n", tempdisplay->student_number);
        tempdisplay = tempdisplay->next;
        }
    }
    printf("\n\nPress any key to go back main menu\n");
    getch();
    }
    
  3. 这是整个代码

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct node
    {
        char student_name[40];
        char student_gender[40];
        char student_course[40];
        int student_number;
        struct node *next;
        struct node *prev;
    }*newnode, *studentlist,*temp,student;
    
    void insert_last(){
    
    }
    
    void print_begin(){
    
        struct node *tempdisplay;
        tempdisplay = studentlist;
    
        if (studentlist == NULL)
            printf("List is empty\n");
        else
        {
            while (tempdisplay!= NULL)
            {
                printf("student name   = %s", tempdisplay->student_name);
                printf("student gender = %s", tempdisplay->student_gender);
                printf("student course = %s", tempdisplay->student_course);
                printf("Student Number = %d\n\n", tempdisplay->student_number);
                tempdisplay = tempdisplay->next;
            }
        }
        printf("\n\nPress any key to go back main menu\n");
        getch();
    }
    
    void print_last(){
        struct node *tempdisplay;
        tempdisplay = studentlist;
    
        if (studentlist == NULL)
            printf("List is empty\n");
        else
        {
            while (tempdisplay->next != NULL)
            {
                tempdisplay = tempdisplay->next;
            }
            while (tempdisplay != NULL)
            {
                printf("student name   = %s", tempdisplay->student_name);
                printf("student gender = %s", tempdisplay->student_gender);
                printf("student course = %s", tempdisplay->student_course);
                printf("Student Number = %d\n\n", tempdisplay->student_number);
                tempdisplay = tempdisplay->prev;
            }
        }
        printf("\n\nPress any key to go back main menu\n");
        getch();
    }
    
    void remove_student(){
    
    }
    
    void insert_new(char name[], char xgender[], char xcourse[], int number){
        newnode = NULL;
        newnode = (struct node*)malloc(sizeof (struct node));
        strcpy(newnode->student_name,name);
        strcpy(newnode->student_gender, xgender);
        strcpy(newnode->student_course, xcourse);
        newnode->student_number = number;
        newnode->next = NULL;
        newnode->prev = NULL;
    
        if (studentlist == NULL)
        {
            studentlist = newnode;
        }
        else
        {
            while (studentlist->next != NULL)
            {
                studentlist = studentlist->next;
            }
            newnode->prev = studentlist;
            studentlist->next = newnode;
        }
    
        printf("Insert success\n");
        printf("Press any key to go back main menu\n");
        getch();
    }
    
    main()
    {
        studentlist=NULL;
        char name[40];
        char gender[40];
        char course[40];
        int number = 0;
        int main_choice;
    
        do{
            printf("\n");
            printf("\n");
            printf("\n");
            printf("               1.Add a new student to the beginning\n");
            printf("               2.Add a new student to the end\n");
            printf("               3.Print out the entire list from the beginning\n");
            printf("               4.Print out the entire list from the end\n");
            printf("               5.Remove a student from the list\n");
            printf("               6.Quit the program\n");
            printf("\n\nEnter the number of the options\n");
            scanf("%d", &main_choice);
            switch (main_choice){
            case 1: {
                        printf("Enter the name of the student\n");
                        fgets(name, 100, stdin);
                        printf("Enter the gender of the student\n");
                        fgets(gender,100,stdin);
                        printf("Enter the course of the student\n");
                        fgets(course,100,stdin);
                        number++;
    
                        insert_new(name,gender,course,number);
            }break;
            case 2: insert_last(); break;
            case 3: print_begin(); break;
            case 4: print_last(); break;
            case 5: remove_student(); break;
            case 6:{
                       printf("\n\n\nThe program is closing...\n...\n");
                       return 0;
                    }break;
            }
            system("cls");
        } while (main_choice != 6);
    }
    

1 个答案:

答案 0 :(得分:0)

您遇到问题的原因是

  1. 与评论中提到的@pmg相同的理由。 fgets读取由于scanf而留下的空行。要解决此问题,请使用fgets读取mainchoice到临时char缓冲区。使用atoi将其转移到int并存储在x中。其余的程序将是相同的。尝试avoid scanf

  2. insert_new中,您正在更改studentlist指针。因此,这从开始打印时会导致问题。使用临时变量直到结束。不要改变studentlist。应始终指向列表的开头。以与print函数中相同的方式执行。