首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >gets()不起作用

gets()不起作用
EN

Stack Overflow用户
提问于 2009-10-01 08:11:23
回答 3查看 8.2K关注 0票数 2

我有一个用C语言编写的程序,当用户选择3这个选项时,它会从一个开关调用get()。它似乎不会等待用户输入某些内容。相反,程序在切换中继续。

代码语言:javascript
复制
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");
}
EN

回答 3

Stack Overflow用户

发布于 2009-10-01 08:33:34

当您通过scanf()调用输入选项时,您需要在键盘上键入2个键,例如3,然后按ENTER键。

scanf()使用“3”,但使ENTER挂在输入缓冲区中。

稍后,当您执行gets()时,ENTER仍然在输入缓冲区中,这就是gets()得到的结果。

您有两个选择:

在每个gets()之前,每次scanf()

  • clear输入缓冲区之后,
  • 清除输入缓冲区

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

代码语言:javascript
复制
int clear_input_buffer(void) {
    int ch;
    while (((ch = getchar()) != EOF) && (ch != '\n')) /* void */;
    return ch;
}

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

票数 9
EN

Stack Overflow用户

发布于 2009-10-01 08:33:56

当你使用scanf("%d",....)读取一个数字时,你在数字后面键入的换行符仍然在那里,在输入缓冲区中等待,当你的程序稍后到达时,它会得到。读到的行将是一个非常短的行,只由换行符组成。

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

代码语言:javascript
复制
while (getchar() != '\n')
    ;

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

票数 2
EN

Stack Overflow用户

发布于 2009-10-01 08:24:55

在scanf行中添加"\n“!gets根据您的选择读取空字符串和CR。

代码语言:javascript
复制
    scanf("%d\n", &userChoice);

在GetField()中,在printf之后,输入"fflush":

代码语言:javascript
复制
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");
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1502562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档