如何从输入不正确输入prevent用户? [英] How to prevent users from typing incorrect inputs?

查看:140
本文介绍了如何从输入不正确输入prevent用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该程序要求的数字。该方案应循环 scanf()的功能如果用户键入别的以外的号码。

在code是,

  {做
     的printf(请输入行数\\ n);
     scanf函数(%d个,&安培;排);
  }而(行> ='A'和;&安培;排< ='Z');

但上面的code不起作用。在信中打字的时候我总是收到一个错误。我试图操纵绕过它,整个事情无限循环。

有什么建议?

另外我怎么能告诉 C编译器不打破循环,除非输入是INTEGER?


解决方案

您所忽略的从返回scanf函数(它告诉你输入的信息是否是准确的转化率(%d个)或不。如果是不准确的,你要做的错误恢复,这是特别不容易与 scanf()的。大多数人去的读一行输入,然后分析它的做法是,如果错误恢复更加简单。



  

据我所知,返回值作为错误检查必不可少的,但我怎么如果它的数字或字母扫描?我可以说,如果(输入!=(整数))或任何类似?


这就是为什么人们不使用 scanf()的。如果你得到的数据行到缓冲区(字符数组),那么你可以经常为你喜欢检查数组的内容。如果你使用 scanf()的,你不会得到一个可靠的机会来处理数据后才 scanf()的决定它有一个错误。

该功能在&LT(通常也可作为宏);文件ctype.h> 允许你进行分类的字符。在功能<文件stdlib.h方式> 提供了从字符串转换可靠,以各种各样的整数

那么,你可以考虑一下做这样的事情:

 字符缓冲区[256];而(与fgets(缓冲区,缓冲区尺寸,标准输入))
{
    ...使用ISDIGIT检查缓冲区的内容()等等......
    ...或者只是提前犁...
    长期价值;
    字符*结束;
    错误号= 0;
    值= strtol将(缓冲液,及放大器;一端,0);
    !如果(错误= 0 ||(*年底='\\ 0'和;!&安培;!isspace为(*结束))
        ...诊断问题...
}



  

这code是有点出我的联赛,此刻..有一个简单的方法?


好吧,我想你可以使用的atoi()而不是与strtol(),简化了错误处理(因为它是不太precise):

 字符缓冲区[256];而(与fgets(缓冲区,缓冲区尺寸,标准输入))
{
    int值=的atoi(缓冲);
    如果(价值== 0)
    {
        看跌期权(零读 - 退出循环);
        打破;
    }
}

它没有得到比这更简单。我不知道你觉得超出你的previous解决方案的哪一部分。替代方案,在我看来,得多fiddlier,涉及每次读一个字符和保存数字,并拒绝非位数:

 字符缓冲区[256];
字符* DST =缓冲;
INT℃;
int值;而((C =的getchar())!= EOF)
{
    如果(ISDIGIT(c))的
    {
        * DST ++ = C; / *忽略缓冲区溢出 - 坏了! * /
    }
    否则如果(isspace为(c))的
    {
        * DST ='\\ 0';
        值=的atoi(缓冲);
        打破;
    }
    其他
    {
        的printf(意外字符%C的'\\ n!,C);
    }
}

诸如此类。有在code,以解决各种问题 - 就像一个错误的字符后重置指针,避免缓冲区溢出,以及处理上的数字标牌,和...好,东西各种各样,我宁愿离开像程序与fgets()与strtol()

The program asks for a number. The program should loop the scanf() function if the user types anything else other than numbers.

The code is,

 do{
     printf("Enter row number\n");
     scanf("%d",&row);
  }while(row>='a' && row<='z');

But the above code doesn't work. I keep getting an error when typing in a letter. I tried manipulating around it and the whole thing loops infinitely.

Any suggestions?

Also how can I tell the C Compiler not to break the loop unless the input is an INTEGER?

解决方案

You are ignoring the return from scanf() which tells you whether the typed information was accurate for the conversion (%d) or not. If it was inaccurate, you have to do error recovery, which is not particularly easy with scanf(). Most people go for the approach of 'read a line of input and then parse it', where the error recovery is simpler.


I understand that return values as essential in error checking, but how do I scan if its numbers or letters? Can I say if (input!=(integers)) or anything similar?

This is why people don't use scanf(). If you get the line of data into a buffer (character array), then you can check the contents of the array as often as you like. If you use scanf(), you don't get a reliable chance to process the data until after scanf() decides it has an error.

The functions (usually also available as macros) in <ctype.h> allow you to classify characters. The functions in <stdlib.h> provide reliable conversions from strings to integers of various sorts.

So, you can think about doing something like:

char buffer[256];

while (fgets(buffer, sizeof(buffer), stdin))
{
    ...check contents of buffer using isdigit() etc...
    ...or just plough ahead with...
    long value;
    char *end;
    errno = 0;
    value = strtol(buffer, &end, 0);
    if (errno != 0 || (*end != '\0' && !isspace(*end))
        ...diagnose problems...
}


This code is a bit out of my league at the moment .. is there a simpler way?

Well, I suppose you can use atoi() instead of strtol(), which simplifies the error handling (because it is less precise):

char buffer[256];

while (fgets(buffer, sizeof(buffer), stdin))
{
    int value = atoi(buffer);
    if (value == 0)
    {
        puts("zero read - exiting loop");
        break;
    }
}

It doesn't get much simpler than this. I don't know which part of the previous solution you felt was beyond you. The alternatives, it seems to me, are much fiddlier, involving reading one character at a time and saving the digits and rejecting the non-digits:

char buffer[256];
char *dst = buffer;
int c;
int value;

while ((c = getchar()) != EOF)
{
    if (isdigit(c))
    {
        *dst++ = c;  /* Ignoring buffer overflow - bad! */
    }
    else if (isspace(c))
    {
        *dst = '\0';
        value = atoi(buffer);
        break;
    }
    else
    {
        printf("Unexpected character '%c'!\n", c);
    }
}

Etcetera. There are various issues to resolve in that code - like resetting the pointer after an erroneous character, and avoiding buffer overflow, and dealing with signs on the numbers, and ... well, all sorts of stuff that I'd rather leave to routines like fgets() and strtol().

这篇关于如何从输入不正确输入prevent用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆