我有一个用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");
}发布于 2009-10-01 08:33:34
当您通过scanf()调用输入选项时,您需要在键盘上键入2个键,例如3,然后按ENTER键。
scanf()使用“3”,但使ENTER挂在输入缓冲区中。
稍后,当您执行gets()时,ENTER仍然在输入缓冲区中,这就是gets()得到的结果。
您有两个选择:
在每个gets()之前,每次scanf()
要清除输入缓冲区,请使用以下代码:
int clear_input_buffer(void) {
int ch;
while (((ch = getchar()) != EOF) && (ch != '\n')) /* void */;
return ch;
}噢!并停止使用 gets().gets()是不可能安全使用的。请改用fgets() 。
发布于 2009-10-01 08:33:56
当你使用scanf("%d",....)读取一个数字时,你在数字后面键入的换行符仍然在那里,在输入缓冲区中等待,当你的程序稍后到达时,它会得到。读到的行将是一个非常短的行,只由换行符组成。
不要使用fflush(stdin),,因为标准并不能保证它能正常工作。相反,您可以只读取循环中的字符,直到跳过换行符:
while (getchar() != '\n')
;您的代码还存在一些其他问题,其中一些问题您实际上根本不应该使用get,因为它不会检查您读取的行是否适合变量。改用fgets。
发布于 2009-10-01 08:24:55
在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");
}https://stackoverflow.com/questions/1502562
复制相似问题