为什么会出现这种接受无效的输入? [英] Why does this accept invalid input?

查看:154
本文介绍了为什么会出现这种接受无效的输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C三角形程序

 的#include<&stdio.h中GT;//它决定三角形并将其打印类型的函数
无效checkTriangle(INT S1,S2 INT,INT S3)
{
    //检查值是否是三角形或不是。
    如果((S1 + S2> S3&放大器;&放大器; S1 + S3> S2&放大器;&放大器; S2 + S3> S1)及及(S1大于0&放大器;&放大器; S2大于0&放大器;&放大器; S3&0))
    {
        //根据给定的输入决定三角形的类型。
        如果(S1 S2 ==&放大器;&安培; S2 == S3)
            的printf(等边三角形);
        否则,如果(S1 S2 = = || S2 S3 == || == S1 S3)
            的printf(等腰三角形\\ n);
        其他
            的printf(不等边三角形\\ n);
    }
    其他
        的printf(\\ nTriangle不能形成。);
}INT主要(无效)
{
    //初始化变量
    诠释A,B,C;    //从用户获取输入
    的printf(请输入三角形的边);    的printf(\\ n请输入侧1:);
    scanf函数(%d个,&安培; A);    的printf(请输入侧2:);
    scanf函数(%D,和b);    的printf(请输入方3:);
    scanf函数(%D,和C);    //为了打印三角形的类型调用函数。
    checkTriangle(A,B,C);
}

当输入为:

  7B

它给出​​了一个错误,这是我想要的,但是当我输入的数据是这样的:

  7
7
7B

它忽略了B,并采取7作为整数 - 但为什么呢?我怎样才能prevent呢?

我想要做的就是给一个错误也为

  7
7
7B


解决方案

如果您希望能够检测与用户的输入错误,如不是一条线的有效十进制整数,那么你可以做到以下几点:


  • 使用读取输入到缓冲区与fgets(缓冲区大小,标准输入)

  • 使用 strtoul将(缓冲,和放大器; endptr,10)来解析缓冲区为十进制整数(基本的 10 ),其中 endptr 的char *

  • endptr 将指向第一个无效字符缓存,即得。最后一个这是成功解析之后的字符

  • 现在,如果 * endptr =='\\ 0',即得。 endptr 缓存的结束时,的的字符串被解析为有效的十进制整数

I have a triangle program in c

#include <stdio.h>

// A function which decides the type of the triangle and prints it
void checkTriangle(int s1, int s2,int s3)
{
    // Check the values whether it is triangle or not.
    if ((s1 + s2 > s3 && s1 +  s3 > s2 && s2 + s3 > s1) && (s1 > 0 && s2 > 0 && s3 > 0))
    {
        // Deciding type of triangle according to given input.
        if (s1 == s2 && s2 == s3)
            printf("EQUILATERAL TRIANGLE");
        else if (s1 == s2 || s2 == s3 || s1 == s3)
            printf("ISOSCELES TRIANGLE\n");
        else
            printf("SCALENE TRIANGLE \n");
    }
    else
        printf("\nTriangle could not be formed.");
}

int main(void)
{
    // Initializing variables
    int a,b,c;

    // Getting input from user
    printf("Please enter the sides of triangle");

    printf("\nPlease enter side 1:");
    scanf("%d",&a);

    printf("Please enter side 2:");
    scanf("%d",&b);

    printf("Please enter side 3:");
    scanf("%d",&c);

    // Calling function in order to print type of the triangle.
    checkTriangle(a,b,c);
}

When the input is:

7b

it gives an error, which is what I want, but when I entered the data like this:

7
7
7b 

it ignores 'b' and take 7 as an integer — but why? How can I prevent this?

What I want to do is give an error also for

7
7
7b

解决方案

If you want to be able to detect an error with the user's input, such as a line not being a valid decimal integer, then you could do the following:

  • Read the input into a buffer using fgets(buffer, size, stdin)
  • Use strtoul(buffer, &endptr, 10) to parse the buffer as a decimal integer (base 10), where endptr is a char*
  • endptr will point to the first invalid character in buffer, ie. the character after the last one which was successfully parsed
  • Now if *endptr == '\0', ie. endptr points to the end of buffer, the whole string was parsed as a valid decimal integer

这篇关于为什么会出现这种接受无效的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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