C 数字验证-无止境循环
我有这个代码来验证用户输入。条件:输入的值只能是零或正数。不接受负值和字母字符 这是我的代码,它一直在循环:C 数字验证-无止境循环,c,validation,C,Validation,我有这个代码来验证用户输入。条件:输入的值只能是零或正数。不接受负值和字母字符 这是我的代码,它一直在循环: #include <stdio.h> int main () { int a, b, c, d; printf ("enter value for a:"); do { b = 0; scanf ("%d", &a); if (!isdigit(a)) { printf("Number mus
#include <stdio.h>
int main ()
{
int a, b, c, d;
printf ("enter value for a:");
do {
b = 0;
scanf ("%d", &a);
if (!isdigit(a)) {
printf("Number must be numeric!:\n");
b++;
}
else if (a < 0) {
printf ("number must be postive\n");
b++;
} else {
printf("\neverything is goood\n");
}
} while (b != 0);
}
#包括
int main()
{
INTA、b、c、d;
printf(“输入a:”的值);
做{
b=0;
scanf(“%d”和“&a”);
如果(!isdigit(a)){
printf(“数字必须是数字!:\n”);
b++;
}
否则,如果(a<0){
printf(“编号必须为正数\n”);
b++;
}否则{
printf(“\n任何东西都是好的\n”);
}
}而(b!=0);
}
您正在使用b作为错误标志
在“一切都好”块中,尝试设置b=0代码>
此外,如果您这样做,您可以摆脱b++代码>行,只需初始化b=1代码>
如果您需要报告用户出错的次数,您应该只执行b++
,我认为您不需要这样做?您使用b作为错误标志
在“一切都好”块中,尝试设置b=0代码>
此外,如果您这样做,您可以摆脱b++代码>行,只需初始化b=1代码>
如果您需要报告用户出错的次数,您应该只执行b++
,我认为您不需要这样做?一旦用户出错,b总是>0。在输入正确的信息后(即在printf(“\neverything is good\n”);语句之后),您需要添加一行设置b=0的语句。只需确保在它前面的else语句中添加{&},这样printf&new b=0;语句将包含在该分支中)一旦用户出错,b总是>0。您需要在输入正确信息后(即在printf(“\neverything is good\n”);语句之后)添加一行设置b=0的语句。只需确保在它前面的else语句中添加{&},这样printf&new b=0;语句将包含在该分支中)isdigit()
需要ASCII编码字符,但是,带有%d
参数的scanf
正在将ASCII编码的数字(字符串)转换为实际数字
也就是说,如果输入'1'
,ASCII码为0x31
,scanf(“%d”),…
会将其转换为值1
。1的ASCII码是而不是数字
要解决此问题,请执行以下操作之一:
- 使
a
键入char
并使用scanf
格式说明符%c
,然后isdigit()
将执行您想要的操作
- 使用
strtol
读入多个字符,并使用char**endptr
参数执行您自己的错误检查
此外,您应该打开编译器警告,并包括包含isdigit()
函数的头文件,ctype.h
isdigit()
需要ASCII编码字符,而scanf
和%d
参数正在转换ASCII编码的数字(字符串)变成一个实际的数字
也就是说,如果输入'1'
,ASCII码为0x31
,scanf(“%d”),…
会将其转换为值1
。1的ASCII码是而不是数字
要解决此问题,请执行以下操作之一:
- 使
a
键入char
并使用scanf
格式说明符%c
,然后isdigit()
将执行您想要的操作
- 使用
strtol
读入多个字符,并使用char**endptr
参数执行您自己的错误检查
此外,您应该打开编译器警告,并包括包含isdigit()
函数的头文件,ctype.h
以下是我可以发现的代码中的一些问题:
您将scanf
与已将输入解析为整数值的%d
格式说明符一起使用。因此,无需使用isdigit
检查输入是否为数字。事实上,isdigit
正在检查十进制数字字符,因此您的用法不正确
您从不检查scanf
return。您应该这样做。如果出现错误(即值为非数字),则不会从流中删除输入。换句话说,您将无法反复解析相同的错误值
您有未使用的变量,并且忘记了包含某些头文件。但在您的情况下,这些是次要的(但在某些情况下可能是主要的!)
也就是说,下面是一些可能适合您的代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
static void skip_input(FILE *stream)
{
int c;
do {
c = fgetc (stream);
} while (c != EOF && c != '\n');
}
int main()
{
int a;
int r;
printf("Please enter a value: ");
for (;;) {
r = scanf("%d", &a);
if (r == EOF)
return EXIT_FAILURE;
if (r != 1) {
printf("Number must be numeric!\n");
skip_input(stdin);
} else if (a < 0) {
printf("Number must be postive\n");
skip_input(stdin);
} else {
printf("Everything is goood\n");
break;
}
}
return EXIT_SUCCESS;
}
#包括
#包括
#包括
静态无效跳过_输入(文件*流)
{
INTC;
做{
c=fgetc(流);
}而(c!=EOF&&c!='\n');
}
int main()
{
INTA;
INTR;
printf(“请输入一个值:”);
对于(;;){
r=scanf(“%d”和&a);
如果(r==EOF)
返回退出失败;
如果(r!=1){
printf(“数字必须是数字!\n”);
跳过输入(标准输入);
}否则,如果(a<0){
printf(“编号必须为正数\n”);
跳过输入(标准输入);
}否则{
printf(“一切都很好”\n);
打破
}
}
返回退出成功;
}
希望能有所帮助。以下是我可以发现的代码中的一些问题:
您将scanf
与已将输入解析为整数值的%d
格式说明符一起使用。因此,无需使用isdigit
检查输入是否为数字。事实上,isdigit
正在检查十进制数字字符,因此您的用法不正确
您从不检查scanf
return。您应该检查。如果出现错误(即值为非数字),则不会从stre中删除输入
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
int main () {
long result;
long len;
int is_valid = 0;
char buf[128];
char *arg;
char *end;
while (!is_valid) {
/* Prompt the user for the integer. */
printf("Enter a non-negative integer: ");
if (fgets(buf, sizeof(buf), stdin) == NULL) {
printf("Giving up so soon?\n");
break;
}
/* Verify that the input doesn't exceed our buffer length. */
len = strlen(buf);
if (buf[len] != '\n') {
printf("Input buffer length exceeded - aborting.\n");
exit(1);
}
/* Skip any leading whitespace. */
for (arg = buf; isspace(*arg); arg++);
/* Attempt to convert the argument. */
errno = 0;
result = strtol(arg, &end, 10);
if (errno == EINVAL) {
printf("Please enter a numeric value.\n");
continue;
}
if (errno == ERANGE) {
printf("Numeric value out of range.\n");
continue;
}
/* Check for non-whitespace characters after the end of the integer. */
for (; isspace(*end); end++);
if (*end != '\0') {
printf("Please enter a numeric value.\n");
continue;
}
/* Verify that the number is non-negative. */
if (result < 0) {
printf("Please enter a positive value.\n");
continue;
}
/* The number is valid. */
printf("Excellent!\n");
is_valid = 1;
}
return 0;
}