gets() 不起作用

发布于 2024-08-06 03:30:31 字数 2864 浏览 6 评论 0原文

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

I have a program written in C and it calls gets() from a switch when a user chooses the option of 3. Here is my code. It does not seem to wait to wait for the user to input something. Rather the program continues in the 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");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

血之狂魔 2024-08-13 03:30:31

当您通过 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()
反而。

When you input an option with the scanf() call, you type 2 keys on your keyboard, for example, 3 and ENTER.
The scanf() consumes the '3' but leaves the ENTER hanging in the input buffer.
When, later, you do gets() that ENTER is still in the input buffer and that's what gets() gets.

You have two options:

  • clear the input buffer after each scanf()
  • clear the input buffer before each gets()

To clear the input buffer use this code:

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

Oh! And stop using gets(). gets() is impossible to use safely. Use fgets()
instead.

云仙小弟 2024-08-13 03:30:31

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

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

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

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

When you read a number using scanf("%d", ....), the newline that you typed after the number is still there, waiting in the input buffer, when your program later gets to the gets. The line that gets reads will be a very short one, consisting of just that newline.

Don't use fflush(stdin), since that is not guaranteed by the standard to work. Instead you can just read characters in a loop until you've skipped the newline:

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

There are also some other problems with your code, among them that you really shouldn't use gets at all, since it doesn't check that the line you read actually fits in the variable. Use fgets instead.

始终不够 2024-08-13 03:30:31

在 scanf 行中添加一个“\n”!
gets 读取空字符串和您选择后的 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");
}

Add a "\n" to the scanf line!
gets reads the empty string and the CR after your choose.

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

And in GetField(), after printf, say "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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文