0

So I am writing a program that has a menu of choices that the user can choose from where they can insert, count, print, delete, and modify nodes. The user also has a 6th option to just quit the program. My issue is that whenever the user chooses 5th option, which is modify the student node, the program just abruptly ends instead of going back to the menu. Why is that and how can I fix this?

Here's the modify function that isn't working properly:

void modifyStudentNode()
{
    int id = 0;
    struct student *ptr, *prev, *currPtr;
    if(start==NULL)
    {
        printf("\nStudent List does not contain data\n");
    }
    else
    {
        printf("\nPlease enter the student ID of the student whose records you wish to modify: \n");
        scanf("%d",&id);
        fflush(stdin);
        for(currPtr = start; (currPtr)&&((currPtr->student_id)!= id); prev = currPtr, currPtr = currPtr->next);

        if(currPtr == NULL)
            printf("\nStudent not Found\n");
        else
        {
            printf("\nCurrent values for Student Id %d are: ", id);
            printf("Student Name: %s\n", currPtr->student_name);
            printf("Student GPA: %.2f\n", currPtr->gpa);
            printf("Student Date of Registration: %d/%d/%d\n\n", currPtr->dt_of_reg.month,currPtr->dt_of_reg.day,currPtr->dt_of_reg.year);
            printf("\nEnter modified values for Student Id %d: \n", id);
            printf("Enter Student's Modified Name:\n");
            scanf("%[^\t\n]s",&currPtr->student_name);
            fflush(stdin);
            printf("Enter Student's Modified GPA:\n");
            scanf("%f",&currPtr->gpa);
            fflush(stdin);
            printf("Enter Student's Modified Date of Registration (mm/dd/yyyy):\n");
            scanf("%d/%d/%d", &currPtr->dt_of_reg.month,currPtr->dt_of_reg.day,currPtr->dt_of_reg.year);
            fflush(stdin);
        }
    }
}
A aqua
  • 15
  • 1
  • 7
  • 1
    What do you mean by "ends"? Is there a runtime error? Does it exit with non-zero status? Also, what is `start`? – Brian61354270 Apr 02 '20 at 00:13
  • @Brian The program just stops working instead of going back to the menu. All the other functions work perfectly fine, it's just this one that doesn't. Also start is what I'm using as the head of the linked list – A aqua Apr 02 '20 at 00:21
  • `fflush(stdin);` is undefined behavior. `%[^\t\n]s` - the format for `%[` is `%[^\t\n]` - the `s` should be remoevd. There is a newline left in the input - you should read it before calling next `scanf` with `%[^\n]`. – KamilCuk Apr 02 '20 at 00:21
  • @KamilCuk I took out the fflush statements and I removed the s, however am I not sure I understand what you meant with the last part? How do I read the newline left in the input? – A aqua Apr 02 '20 at 00:31
  • https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer – KamilCuk Apr 02 '20 at 01:39
  • Read [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – Basile Starynkevitch Apr 02 '20 at 05:55

1 Answers1

1

Your line

scanf("%d/%d/%d", &currPtr->dt_of_reg.month,currPtr->dt_of_reg.day,currPtr->dt_of_reg.year);

should be

scanf("%d/%d/%d", &currPtr->dt_of_reg.month, &currPtr->dt_of_reg.day, &currPtr->dt_of_reg.year);

You forgot to add the & for the last both numbers.

the busybee
  • 10,755
  • 3
  • 13
  • 30