C - 使用 scanf() 时额外的换行输入
C - Extra new line input while using scanf()
我有下面的 'C' 代码,用于使用 Visual Studio 的控制台 Win32 应用程序。它检查参数并直接调用函数或使用 scanf()
获取输入,然后调用该函数。如果我提供命令行参数,该程序可以完美运行,但是当我使用 scanf()
提供输入时,它可能会添加一个我想避免的额外换行符。
我知道它这样做是因为当我给出参数时,在显示 'Press ENTER to quit...' 之后,它会等待 ENTER 输入,但在使用 scanf()
时不会这样做。如果我也使用 scanf_s()
就会出现此问题。
感谢您对此的帮助。
此致,
沙希德
unsigned long int ipv;
unsigned int yrs;
float acr, acl, asp, irr, psp;
if(argc<=1) {
//get inputs
printf("Initial value of portfolio: "); scanf("%lu", &ipv);
printf("Average cash rate: "); scanf("%f", &acr);
printf("Average cost of living: "); scanf("%f", &acl);
printf("Average return for share portfolio: "); scanf("%f", &asp);
printf("Initial removal for living expenses: "); scanf("%f", &irr);
printf("Percentage of shares in initial portfolio: "); scanf("%f", &psp);
printf("Years for the analysis: "); scanf("%u", &yrs);
//simulate the investment with given inputs
simulate(ipv, acr, acl, asp, irr, psp, yrs);
} else {
if (argc == 8) {
//invsim 1000000 0.05 0.036 0.12 0.08 0.5 20
//simulate the investment with given inputs
simulate(atol(argv[1]), (float)atof(argv[2]), (float)atof(argv[3]), (float)atof(argv[4]), (float)atof(argv[5]), (float)atof(argv[6]), atoi(argv[7]));
} else {
printf("Please enter arguments as...\ninvsim.exe ipv acr acl asp irr psp yrs");
}
}
printf("\n\nPress ENTER to quit...");
getchar();
I know it does this because when I give arguments, after showing 'Press ENTER to quit...', it waits for the ENTER input, but doesn't do so when using scanf().
这是正确的行为,不是吗?当您通过命令行提供参数时,您没有按任何 ENTER 键。因此,输入缓冲区中没有 \n
。因此,getchar()
将等待来自 stdin
.
的输入
但是,当您使用 scanf()
获取值时,您在输入后按 ENTER 键。对于数值,扫描数值但 \n
(通过点击 ENTER 产生)保留在输入缓冲区中以供下一个 getchar()
读取。因此,您的代码不会 wait.
解决:在scanf()
部分代码的末尾添加一个getchar();
,即在if
块的末尾。
要改进您的 "pause" 功能,请使用以下更新:
printf("\n\nPress ENTER to quit...");
while( getchar() != '\n' ); // clear the input buffer after reading numbers with scanf
getchar(); // wait for new input
编辑:
考虑到您程序的 if-else
,将建议的 while
循环移动到上面:
printf("Years for the analysis: ");
scanf("%u", &yrs);
while( getchar() != '\n' ); // clear the input buffer
我也 运行 关注这个问题,它是 C 中最令人沮丧的部分之一。我不记得我到底做了什么,但我记得我最终写了一个小库来处理每种类型的输入,即字符串、数字等。
IIRC,我只是使用 getline() 并使用 atoi() 和 ctype.h 中的其他转换函数将字符串解析为我需要的类型;我检查了字符串的长度,如果它是 1(?),唯一的方法是缓冲区中是否已经有新行,所以我再次调用 getline()。
这样,我可以一次编写输入验证,并使用它一百万次。所以我的代码最终看起来像:
do {
get_int("Enter an integer:", &myInt);//returns -123456789 for invalid input, or such
} while ( myInt <= 0 )//assuming you only wanted positive input
这还有一个重要的好处,即当您输入一个字符串并且 scanf() 需要一个整数时,您的程序不会崩溃。
我有下面的 'C' 代码,用于使用 Visual Studio 的控制台 Win32 应用程序。它检查参数并直接调用函数或使用 scanf()
获取输入,然后调用该函数。如果我提供命令行参数,该程序可以完美运行,但是当我使用 scanf()
提供输入时,它可能会添加一个我想避免的额外换行符。
我知道它这样做是因为当我给出参数时,在显示 'Press ENTER to quit...' 之后,它会等待 ENTER 输入,但在使用 scanf()
时不会这样做。如果我也使用 scanf_s()
就会出现此问题。
感谢您对此的帮助。
此致,
沙希德
unsigned long int ipv;
unsigned int yrs;
float acr, acl, asp, irr, psp;
if(argc<=1) {
//get inputs
printf("Initial value of portfolio: "); scanf("%lu", &ipv);
printf("Average cash rate: "); scanf("%f", &acr);
printf("Average cost of living: "); scanf("%f", &acl);
printf("Average return for share portfolio: "); scanf("%f", &asp);
printf("Initial removal for living expenses: "); scanf("%f", &irr);
printf("Percentage of shares in initial portfolio: "); scanf("%f", &psp);
printf("Years for the analysis: "); scanf("%u", &yrs);
//simulate the investment with given inputs
simulate(ipv, acr, acl, asp, irr, psp, yrs);
} else {
if (argc == 8) {
//invsim 1000000 0.05 0.036 0.12 0.08 0.5 20
//simulate the investment with given inputs
simulate(atol(argv[1]), (float)atof(argv[2]), (float)atof(argv[3]), (float)atof(argv[4]), (float)atof(argv[5]), (float)atof(argv[6]), atoi(argv[7]));
} else {
printf("Please enter arguments as...\ninvsim.exe ipv acr acl asp irr psp yrs");
}
}
printf("\n\nPress ENTER to quit...");
getchar();
I know it does this because when I give arguments, after showing 'Press ENTER to quit...', it waits for the ENTER input, but doesn't do so when using scanf().
这是正确的行为,不是吗?当您通过命令行提供参数时,您没有按任何 ENTER 键。因此,输入缓冲区中没有 \n
。因此,getchar()
将等待来自 stdin
.
但是,当您使用 scanf()
获取值时,您在输入后按 ENTER 键。对于数值,扫描数值但 \n
(通过点击 ENTER 产生)保留在输入缓冲区中以供下一个 getchar()
读取。因此,您的代码不会 wait.
解决:在scanf()
部分代码的末尾添加一个getchar();
,即在if
块的末尾。
要改进您的 "pause" 功能,请使用以下更新:
printf("\n\nPress ENTER to quit...");
while( getchar() != '\n' ); // clear the input buffer after reading numbers with scanf
getchar(); // wait for new input
编辑:
考虑到您程序的 if-else
,将建议的 while
循环移动到上面:
printf("Years for the analysis: ");
scanf("%u", &yrs);
while( getchar() != '\n' ); // clear the input buffer
我也 运行 关注这个问题,它是 C 中最令人沮丧的部分之一。我不记得我到底做了什么,但我记得我最终写了一个小库来处理每种类型的输入,即字符串、数字等。
IIRC,我只是使用 getline() 并使用 atoi() 和 ctype.h 中的其他转换函数将字符串解析为我需要的类型;我检查了字符串的长度,如果它是 1(?),唯一的方法是缓冲区中是否已经有新行,所以我再次调用 getline()。
这样,我可以一次编写输入验证,并使用它一百万次。所以我的代码最终看起来像:
do {
get_int("Enter an integer:", &myInt);//returns -123456789 for invalid input, or such
} while ( myInt <= 0 )//assuming you only wanted positive input
这还有一个重要的好处,即当您输入一个字符串并且 scanf() 需要一个整数时,您的程序不会崩溃。