gets()不起作用

时间:2009-10-01 08:11:23

标签: c gets

我有一个用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");
}

3 个答案:

答案 0 :(得分:9)

当您通过scanf()来电输入选项时,您在键盘上键入2个键,例如3和ENTER。
scanf() 消耗'3'但将ENTER保留在输入缓冲区中。
稍后,当您gets()时,ENTER仍然在输入缓冲区中,这就是gets()得到的内容。

您有两种选择:

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

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

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

哦! 停止使用gets() gets()无法安全使用。 使用fgets() 代替。

答案 1 :(得分:2)

当您使用 scanf(“%d”,....)读取数字时,您输入的换行符仍然存在,在输入缓冲区中等待您的程序稍后进入获取。获取读取的行将非常短,仅包含该换行符。

不要使用 fflush(stdin),因为标准无法保证这一点。相反,您可以直接读取循环中的字符,直到您跳过换行符:

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

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

答案 2 :(得分:-1)

在scanf行添加“\ n”! 在你选择之后获取空字符串和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");
}