C get()不起作用

C get()不起作用,c,gets,C,Gets,我有一个用C编写的程序,当用户选择3选项时,它会从开关调用get()。这是我的密码。它似乎并不等待用户输入一些东西。相反,程序在交换机中继续 void getField(); #include <stdio.h> #include <string.h> /*#include "stubs.c" #include "record.h" */ int debugMode; void getField(){ char name[25]; char addr

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

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");
}
void getField();
#包括
#包括
/*#包括“stubs.c”
#包括“record.h”*/
int调试模式;
void getField(){
字符名[25];
字符地址[80];
出生年份;
char-telno[15];
int计数器=0;
if(debugMode==1){
printf(“***调试***输入getField函数\n”);
}
printf(“输入您的姓名:”);
获取(名称);
printf(“名称:%s\n”,名称);
printf(“\n”);
}
void main(int argc,char*argv[])
{
结构记录*start=NULL;
int用户选择;
调试模式=0;
如果(argv[1]!=NULL){
if((strcmp(argv[1],“debug”)==0)和&(argv[2]==NULL)){
调试模式=1;
printf(“欢迎使用个人通讯簿应用程序”);
}
否则{
int i=0;
while(argv[i]!=NULL){
printf(argv[i]);
printf(“”);
i++;
}
printf(“:未找到命令\n”);
用户选择=6;
}
}
如果(argv[1]==NULL){
printf(“欢迎使用个人通讯簿应用程序”);
userChoice=0;
}
while(userChoice!=6)
{
if(debugMode==1){
printf(“***调试***输入do while循环\n”);
}
printf(“为下面的选项输入相应的编号\n\n”);
printf(“1)在数据库中添加新记录\n”);
printf(“2)修改数据库中的记录\n”);
printf(“3)打印有关数据库中记录的信息\n”);
printf(“4)打印数据库中的所有信息\n”);
printf(“5)从数据库中删除现有记录\n”);
printf(“6)退出程序\n\n>”;
scanf(“%d”、&userChoice);
开关(用户选择){
案例1:
/*addRecord(开始,arrayHolder,arrayHolder,0,arrayHolder);
*/userChoice=0;
打破
案例2:
/*修改记录(开始、阵列文件夹、阵列文件夹、阵列文件夹);
*/userChoice=0;
打破
案例3:
/*打印记录(开始、阵列文件夹);
*/userChoice=0;
getField();
打破
案例4:
/*打印所有记录(开始);
*/userChoice=0;
打破
案例5:
/*删除记录(开始、阵列文件夹);
*/userChoice=0;
打破
案例6:
printf(“案例6\n”);
打破
违约:
printf(“默认值”);
userChoice=0;
打破
}
}
printf(“\n”);
}
在扫描行中添加“\n”! 获取在选择后读取空字符串和CR

    scanf("%d\n", &userChoice);
在GetField()中,在printf之后说“fflush”:


使用
scanf()
调用输入选项时,您可以在键盘上键入2个键,例如3和ENTER。
scanf()
使用'3',但保留输入缓冲区中的ENTER挂起。
稍后执行
gets()
时,ENTER仍在输入缓冲区中,这就是
gets()
获取的内容

您有两个选择:

  • 在每次
    scanf()之后清除输入缓冲区
  • 在每次
    获取()之前清除输入缓冲区
要清除输入缓冲区,请使用以下代码:

int clear_input_buffer(void) {
    int ch;
    while (((ch = getchar()) != EOF) && (ch != '\n')) /* void */;
    return ch;
}
哦!!并且停止使用
get()
<代码>获取()
不可能安全使用使用
fgets()

相反。

当您使用scanf(“%d”,…)读取一个数字时,您在该数字之后键入的换行符仍在那里,等待在输入缓冲区中,当您的程序稍后到达时,它将获得。获取reads的行将是一个非常短的行,仅由该新行组成

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

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

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

+1表示有关
获取的警告。非常危险,以至于C标准实际上不赞成它。(不过,他们可能会反对更多的建议…)第一个问题:您正在调用get()。
while (getchar() != '\n')
    ;