Search code examples
cstructfgets

Fgets get ignored & program only read 2 input data (linked list)


  1. My first fgets get ignored when i run it, it straight jump to the second fgets why it is?i expect it will the fgets to get the user input for student name but it doesn't.So i cannot get the name for student

    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. My print_begin() function could print out 2 input data which mean if the studentlist have 4 struct it only print out two. But my print_last work well to print all the input.

    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();
    }
    

this is the whole code

#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);
}

Solution

  • The reasons for your problems are

    1. Same reason as @pmg mentioned in comments. fgets reads an empty line left over due to scanf. To solve this, use fgets to read the mainchoice to temporary char buffer. Use atoi to convet it to int and store in x. Rest of the program will be same. Try to avoid scanf.

    2. In insert_new, you are changing the studentlist pointer. So this causes problem while printing from start. Use a temporary variable to reach till the end. Dont alter studentlist. It should always point to start of the list. Do in the same way as you did in print function.