【问题标题】:gets() does not work获取()不起作用
【发布时间】:2023-03-03 14:48:01
【问题描述】:

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

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");
}

【问题讨论】:

  • 第一个问题:你正在调用gets()。

标签: c gets


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    当您通过 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 用于警告 gets。如此危险,以至于 C 标准实际上已弃用它。 (但是,他们可能会弃用更多……)
    【解决方案3】:

    在 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");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2016-01-03
      • 2023-03-08
      • 2011-10-15
      • 2015-02-12
      相关资源
      最近更新 更多