fgets()不等待输入

时间:2016-03-17 13:59:25

标签: c fgets

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

我尝试了here建议的解决方案,但在scanf("%d *[^\n]", &option);中使用menu()似乎对我不起作用。它可能是造成这个问题的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:

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

1 个答案:

答案 0 :(得分:0)

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