[英]C programming. Trying to loop trap a user into entering a positive number
Beginner here. 初学者在这里。 I'm trying to trap a user into entering a positive number.
我正试图诱使用户输入正数。 However the while loop doesn't seem to be working for when the user enters and incorrect number.
但是,当用户输入错误的数字时,while循环似乎不起作用。
Output: 输出:
Please enter a positive integer: -3
I'm sorry, you must enter a positive integer greater than zero: why?
I'm sorry, you must enter a positive integer greater than zero: -42
I'm sorry, you must enter a positive integer greater than zero: 42
The positive integer was : 42
Press any key to continue....
Code: 码:
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
void clear_keyboard_buffer(void);
int main()
{
int k;
printf("Please enter a positive integer: ");
scanf("%d", &k);
if (k > 0)
{
clear_keyboard_buffer();
printf("The positive integer was: %d ", k);
}
while (k<=0)
{
printf("I'm sorry, you must enter a positive integer greater than zero: ");
scanf("%d", &k);
return 0;
}
}
void clear_keyboard_buffer(void)
{
char ch;
scanf("%c", &ch);
while (ch != '\n')
{
scanf("%c", &ch);
}
}
Ok, I think it's easiest to show you some code that does what you want, along with some comments about why you should do it that way: 好的,我认为向您展示一些满足您需求的代码是最简单的,并附上一些有关您为什么要这样做的注释:
#include <stdio.h>
int main(void) /* note the void here, it says "no parameters"! */
{
int k;
/* here we don't use printf() because there is no formatting to do */
fputs("Please enter a positive integer: ", stdout);
scanf(" %d", &k); /* note the space, it consumes any whitespace */
while (k < 1)
{
fputs("I'm sorry, you must enter a positive integer greater "
"than zero: ", stdout);
scanf(" %d", &k);
}
printf("You entered `%d'\n", k);
return 0;
}
Still you should check the return value of scanf()
for production quality code because there could be errors (eg the user entering something that isn't a number ...) 仍然应该检查
scanf()
的返回值以获取生产质量代码,因为可能存在错误(例如,用户输入的不是数字...)
That being said, for really reliable user input, I'd suggest to abandon scanf()
altogether and just use eg fgets()
to read a line of input (whatsoever) and then parse yourself. 话虽如此,对于真正可靠的用户输入,我建议完全放弃
scanf()
而仅使用fgets()
来读取输入行(无论如何),然后解析自己。 strtol()
could come handy ... strtol()
可以派上用场...
Just to give you an idea what I'm talking about, here's a very simple but reliable solution: 只是为了让您了解我在说什么,这是一个非常简单但可靠的解决方案:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int readInt(void)
{
char buf[1024];
int input;
char *endptr;
fgets(buf, 1024, stdin);
/* strip off "end of line" characters */
buf[strcspn(buf, "\r\n")] = 0;
input = strtol(buf, &endptr, 10);
/* sanity check, only accept inputs that can be wholly parsed as
* an integer
*/
if (buf[0] == 0 || *endptr != 0) input = -1;
return input;
}
int main(void)
{
int k;
fputs("Please enter a positive integer: ", stdout);
k = readInt();
while (k < 1)
{
fputs("I'm sorry, you must enter a positive integer greater "
"than zero: ", stdout);
k = readInt();
}
printf("You entered `%d'\n", k);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.