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");
}
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您通过
scanf()
调用输入选项时,您需要在键盘上键入 2 个键,例如 3 和 ENTER。scanf()
消耗“3”,但将 ENTER 保留在输入缓冲区中。稍后,当您执行
gets()
时,ENTER 仍在输入缓冲区中,这就是gets()
获取的内容。您有两种选择:
scanf()
之后清除输入缓冲区gets()
之前清除输入缓冲区要清除输入缓冲区,请使用以下代码:
哦!并且停止使用
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 whatgets()
gets.You have two options:
scanf()
gets()
To clear the input buffer use this code:
Oh! And stop using
gets()
.gets()
is impossible to use safely. Usefgets()
instead.
当您使用 scanf("%d", ....) 读取数字时,您在数字后键入的换行符仍然存在,在输入缓冲区中等待,当您的程序稍后到达时获得。读取的行将非常短,仅包含换行符。
不要使用 fflush(stdin),因为标准不能保证它可以工作。相反,您可以循环读取字符,直到跳过换行符:
您的代码还存在一些其他问题,其中您根本不应该使用 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:
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.
在 scanf 行中添加一个“\n”!
gets 读取空字符串和您选择后的 CR。
在 GetField() 中,在 printf 之后说“fflush”:
Add a "\n" to the scanf line!
gets reads the empty string and the CR after your choose.
And in GetField(), after printf, say "fflush":