fgets() 不等待输入

fgets() does not wait for input

下面的程序不允许用户输入学生姓名。最初,我使用 scanf() 而不是 fgets() 来存储输入,因为 scanf() 不存储空格。 (原节目

我尝试了 here 建议的解决方案,但在 menu() 中使用 scanf("%d *[^\n]", &option); 似乎对我不起作用。会不会是 ID 号生成器部分导致了这个问题?

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>

    #define MAX 200
    #define ID_SIZE 20
    #define NAME_SIZE 50
    #define ADDRESS_SIZE 80
    #define TEL_SIZE 15

    int i = 0;
    int total = 0;
    int result;
    char ID[ID_SIZE];

    int menu();
    int add_ID();
    ...         

    struct Student{
        char stud_ID[ID_SIZE];
        char name[NAME_SIZE];
        char address[ADDRESS_SIZE];
        char tel_no[TEL_SIZE];
        float marks; 
    } stud [MAX];

    int main() {
        menu();
        getch();
        return 0;
    }

    int menu() {

        int option;
            printf ("1. Insert new student\n\n");
            printf ("2. Search for a particular student by ID number\n\n");
...
            printf ("Please enter your option: ");
            scanf ("%d", &option);

            while ((option < 1 ) || (option > 6)){
                printf("Please reenter your option: ");
                scanf ("%d", &option);
            }

            switch (option) {
                case 1 : add_ID();
                break;
                case 2 : search_ID();
                break;
...
                case 6 : printf("Exit\n");
                         exit(0);
                break;
                getch();
                break;
            }
        return 0;
    }

    int add_ID () {
        char next;

        printf("\n>> Add new student <<\n\n");

        do {
            sprintf(ID, "%d", i+1);
            strcpy(stud[i].stud_ID, ID);
            printf("Student ID: %s\n", stud[i].stud_ID);

            printf("Enter Name: ");
            fgets(stud[i].name, NAME_SIZE, stdin) ;

            printf("Enter address: ");
            fgets(stud[i].address, ADDRESS_SIZE, stdin) ;

            printf("Enter telephone number: ");
            fgets(stud[i].tel_no, TEL_SIZE, stdin) ;

            printf("Enter marks: "); 
            scanf("%f", &stud[i].marks);

            printf("Do you want to add another student? (y/n):\n"); 
            scanf("%s", &next);

                i++;            
                total = i;

            } while (((next == 'y') || (next == 'Y')) && (i < MAX));

            if ((next == 'n') || (next == 'N')){
                printf("Total students: %d\n\n", total);
                menu();
                getch();
            }

        return 0;
    }

    int search_ID() {
        printf("\n>> Search for student using ID <<\n\n");
        printf("Please enter the student ID: ");
        scanf("%s", ID);
        display_Header();           

        for (i = 0; i < total; i++) {
            result = strcmp(stud[i].stud_ID, ID);

            if (result == 0){
                display_Info(i);
                break;
            } 
        }
        if (i == total) {
                printf ("Not found\n");
        }

        menu();
        getch();        
        return 0;
    }
...

以下是选择选项 1 后程序的输出:

Student ID: 1
Enter name: Enter address:

程序不等待键盘输入,立即执行后面的语句。

尝试使用 fflush(stdin);在你扫描

之后