0

I am trying to make a menu that allows the user to access various binary files and create, adjust, read and delete them. I have majority of the code done, but I am running into an issue where when the user inputs an invalid command the programs spins off into an infinite loop. I have tried various things to no avail, would someone please help.

int main()
{

int input;

    while (1)
    {
    int titleSpace = 48;
    char *title = "M A I N  M E N U";
    printf ("%*s\n", titleSpace / 2 + strlen(title) / 2, title);

    printf ("\t1. Create the Binary File\n");
    printf ("\t2. Display the contents of the file\n");
    printf ("\t3. Seek a specific record\n");
    printf ("\t4. Update the contents of a record\n");
    printf ("\t5. Delete a record for the specific name\n");
    printf ("\t6. Exit\n");
    printf ("\n");

    int choiceSpace = 48;
    char *choice = "Please Enter your choice.... ";
    printf ("%*s", choiceSpace / 2 + strlen(choice) / 2, choice);
    scanf("%d", &input);
    printf ("\n");

    switch (input)
      {
        case 1:
            writeRecord();
            printf ("\n");
            break;

        case 2:
            displayRecord();
            printf ("\n");
            break;

        case 3:
            searchRecord();
            printf ("\n");
            break;

        case 4:
            updateRecord();
            printf ("\n");
            break;

        case 5:
            deleteRecord();
            printf ("\n");
            break;

        case 6:
            printf ("********** E x i t i n g  P r o g r a m **********\n");
            exit(0);
            break;
      }
    }

}
  • Did you try using `getch()` as `printf("%c", getch());`. It gets one char but I think other input will be in the buffer and the infinity loop appears again. Also, use default for your switch case. – sajjad rezaei Jan 13 '22 at 21:55

0 Answers0