C fgets()不等待输入

C fgets()不等待输入,c,fgets,C,Fgets,下面的程序不允许用户输入学生的姓名。最初,我使用scanf()而不是fgets()来存储输入,因为scanf()不存储空格。(原计划) 我尝试了建议的解决方案,但使用了scanf(“%d*[^\n]”,&option)似乎对我不起作用。是否是ID号生成器部件导致了此问题 #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <string.h&

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

我尝试了建议的解决方案,但使用了
scanf(“%d*[^\n]”,&option)菜单()中的code>似乎对我不起作用。是否是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;
    }
...

程序不会等待键盘输入并立即执行后续语句

尝试使用fflush(stdin);扫描后

使用
scanf
如tis:
scanf(“%[^\n]”,输入)
@ameyCU如第二段所述,这个问题的答案对我不起作用。我尝试了这个问题的解决方案,但
fflush(stdout)
也不起作用。非常感谢@Elina。成功了!在
stdin
上使用
fflush
会导致未定义的行为,依赖它可能不是一个好主意。
Student ID: 1
Enter name: Enter address: