2

我有一个用 C 编写的程序,当用户选择 3 选项时,它从开关调用 gets()。这是我的代码。它似乎不需要等待用户输入一些东西。而是程序在交换机中继续。

void getField();

#include <stdio.h>
#include <string.h>
/*#include "stubs.c"
#include "record.h" */

int debugMode;

void getField(){
    char name[25];
    char address[80];
    int yearofbirth;
    char telno[15];
    int counter = 0;

    if(debugMode == 1){
        printf("***DEBUG*** Entering getField function \n");
    }

    printf("Enter your name:");
    gets(name);

    printf("Name: %s \n", name);
    printf("\n");
}

void main(int argc, char * argv[])
{
    struct record* start = NULL;
    int userChoice;
    debugMode = 0;

    if(argv[1] != NULL){
        if( (strcmp(argv[1], "debug") == 0) && (argv[2] == NULL) ){
            debugMode = 1;
            printf("Welcome, to the personal address book application \n");
        }
        else{
            int i = 0;
            while(argv[i] != NULL){
                printf(argv[i]);
                printf(" ");
                i++;
            }
            printf(": Command not found \n");
            userChoice = 6;
        }
    }

    if(argv[1] == NULL){
        printf("Welcome, to the personal address book application \n");
        userChoice = 0;
    }


    while(userChoice != 6)
    {
        if(debugMode == 1){
            printf("***DEBUG*** Entering do-while loop \n");
        }

        printf("Enter number corresponding number to option below \n\n");   

        printf("1) Add a new record in the database \n");
        printf("2) Modify a record in the database \n");
        printf("3) Print information about a record in the database \n");
        printf("4) Print all information in the database \n");
        printf("5) Delete an existing record from the database \n");
        printf("6) Quit program \n\n >");


        scanf("%d", &userChoice);

        switch(userChoice){

            case 1:
                /*addRecord(start, arrayHolder, arrayHolder, 0, arrayHolder);
                */userChoice = 0;
                break;
            case 2:
                /*modifyRecord(start, arrayHolder, arrayHolder, arrayHolder);
                */userChoice = 0;
                break;
            case 3:
                /*printRecord(start, arrayHolder);
                */userChoice = 0;
                getField();
                break;
            case 4:
                /*printAllRecords(start);
                */userChoice = 0;
                break;
            case 5:
                /*deleteRecord(start, arrayHolder);
                */userChoice = 0;
                break;
            case 6:
                printf("case 6 \n");
                break;
            default:
                printf("default \n");
                userChoice = 0;
                break;
        }

    }
    printf("\n");
}
4

3 回答 3

9

当您在呼叫中输入选项时scanf(),您在键盘上键入 2 个键,例如 3 和 ENTER。消耗
'3' 但 将ENTER 挂在输入缓冲区中。 稍后,当您这样做时,ENTER 仍然在输入缓冲区中,这就是得到的。scanf()
gets()gets()

你有两个选择:

  • 每次之后清除输入缓冲区scanf()
  • 每次之前清除输入缓冲区gets()

要清除输入缓冲区,请使用以下代码:

int clear_input_buffer(void) {
    int ch;
    while (((ch = getchar()) != EOF) && (ch != '\n')) /* void */;
    return ch;
}

哦!并停止使用gets(). gets()是不可能安全使用的。改为使用fgets()

于 2009-10-01T08:33:34.297 回答
2

当您使用scanf("%d", ....)读取数字时,您在数字之后键入的换行符仍然存在,在输入缓冲区中等待,当您的程序稍后到达gets 时。读取的行将是一个非常短的行,仅包含该换行符。

不要使用fflush(stdin),因为标准不能保证它可以工作。相反,您可以在循环中读取字符,直到您跳过换行符:

while (getchar() != '\n')
    ;

您的代码还有一些其他问题,其中您根本不应该使用gets,因为它不会检查您读取的行是否真正适合变量。请改用fgets

于 2009-10-01T08:33:56.170 回答
-1

在 scanf 行中添加“\n”!get 在您选择后读取空字符串和 CR。

    scanf("%d\n", &userChoice);

在 GetField() 中,在 printf 之后,说“fflush”:

void getField(){
    char name[25];
    char address[80];
    int yearofbirth;
    char telno[15];
    int counter = 0;

    if(debugMode == 1){
        printf("***DEBUG*** Entering getField function \n");
    }

    printf("Enter your name:");
    fflush(stdout);
    gets(name);

    printf("Name: %s \n", name);
    fflush(stdout);
    printf("\n");
}
于 2009-10-01T08:24:55.297 回答