Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 为什么它接受无效的输入?_C - Fatal编程技术网

C 为什么它接受无效的输入?

C 为什么它接受无效的输入?,c,C,我有一个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

我有一个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);
}
它给出了一个错误,这正是我想要的,但当我像这样输入数据时:

7
7
7b 
它忽略“b”并将7作为整数-但为什么?我怎样才能防止这种情况

我想做的是也给一个错误

7
7
7b

%d
只接受整数。尝试使用scanf()中的
%x
进行十六进制输入


最好是以字符串形式输入,然后使用
isnumeric()
进行检查,否则使用
scanf(“%[^\n]s”,word)
像@mou建议的那样。

%d
只接受整数。尝试使用scanf()中的
%x
进行十六进制输入


最好您可以以字符串形式获取输入,然后使用
isnumeric()
进行检查,否则请使用
scanf(“%[^\n]s”,word)
如@mou建议的那样。

您不应该使用scanf或使用scanf(“%[^\n]s”,word);或者使用get()之类的东西
另外,在我的示例末尾,用d或x表示字符串而不是s:P

您不应该使用scanf或do scanf(“%[^\n]s”,word);或者使用get()之类的东西


另外,在我的示例末尾放置d或x,而不是s表示字符串:P

将输入读入字符串缓冲区。解析字符串以逐个提取任何类型的数值。

将输入读入字符串缓冲区。解析字符串以逐个提取任何类型的数值。

如果希望能够检测用户输入的错误,例如某行不是有效的十进制整数,则可以执行以下操作:

  • 使用
    fgets(buffer,size,stdin)将输入读入缓冲区
  • 使用
    strtoul(buffer,&endptr,10)
    将缓冲区解析为十进制整数(base10),其中
    endptr
    是一个
    char*
  • endptr
    将指向
    缓冲区中的第一个无效字符,即成功解析的最后一个字符之后的字符
  • 现在,如果
    *endptr=='\0'
    ,即
    endptr
    指向
    缓冲区的末尾,则整个字符串被解析为有效的十进制整数

如果您希望能够检测用户输入的错误,例如某行不是有效的十进制整数,则可以执行以下操作:

  • 使用
    fgets(buffer,size,stdin)将输入读入缓冲区
  • 使用
    strtoul(buffer,&endptr,10)
    将缓冲区解析为十进制整数(base10),其中
    endptr
    是一个
    char*
  • endptr
    将指向
    缓冲区中的第一个无效字符,即成功解析的最后一个字符之后的字符
  • 现在,如果
    *endptr=='\0'
    ,即
    endptr
    指向
    缓冲区的末尾,则整个字符串被解析为有效的十进制整数

如果您真的希望每个数字都位于单独的输入行上,并且整行都是有效的数字或空格,那么您可能需要忘记
scanf()
和family,而改用
fgets()
strtol()

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <limits.h>

static int read_side(void)
{
    char buffer[4096];
    if (fgets(buffer, sizeof(buffer), stdin) == 0)  // EOF
        return -1;
    char *end;
    errno = 0;
    long result = strtol(buffer, &end, 10);
    if (result < 0 || errno != 0) // Neither errors nor negative numbers are allowed
        return -1;
    if (end == buffer)     // Nothing was convertible
        return -1;
    while (isspace(*end))
        end++;
    if (*end != '\0')      // Non-spaces after the last digit
        return -1;
    if (result > INT_MAX)  // Result too big for `int`
        return -1;
    return result;
}
#包括
#包括
#包括
#包括
#包括
静态整数读取侧(无效)
{
字符缓冲区[4096];
if(fgets(buffer,sizeof(buffer),stdin)==0)//EOF
返回-1;
字符*结束;
errno=0;
长结果=strtol(缓冲区和结束区,10);
if(result<0 | | errno!=0)//既不允许出现错误也不允许出现负数
返回-1;
if(end==buffer)//没有可转换的内容
返回-1;
while(isspace(*end))
end++;
if(*end!='\0')//最后一位数字后没有空格
返回-1;
if(result>INT_MAX)//结果对于'INT'来说太大`
返回-1;
返回结果;
}
(如果您需要接受任何有效的
int
值,但要区分错误,则需要传入指向该函数的指针,并在出错时返回-1,成功时返回0,然后将安全结果分配给指针。)


是的,正确地做这项工作真是太麻烦了。是的,分析strtol()的结果就跟这一样棘手;你必须非常小心。(还有一种可能是我忘了检查可检测的错误情况。)不,我认为你不能用
scanf()
等做同样的工作;特别是,
scanf()
溢出时的行为是未定义的。

如果您确实希望每个数字位于单独的输入行上,并且整行都是有效的数字或空格,那么您可能需要忘记
scanf()
和family,而使用
fgets()
strtol()

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <ctype.h>
#include <limits.h>

static int read_side(void)
{
    char buffer[4096];
    if (fgets(buffer, sizeof(buffer), stdin) == 0)  // EOF
        return -1;
    char *end;
    errno = 0;
    long result = strtol(buffer, &end, 10);
    if (result < 0 || errno != 0) // Neither errors nor negative numbers are allowed
        return -1;
    if (end == buffer)     // Nothing was convertible
        return -1;
    while (isspace(*end))
        end++;
    if (*end != '\0')      // Non-spaces after the last digit
        return -1;
    if (result > INT_MAX)  // Result too big for `int`
        return -1;
    return result;
}
#包括
#包括
#包括
#包括
#包括
静态整数读取侧(无效)
{
字符缓冲区[4096];
if(fgets(buffer,sizeof(buffer),stdin)==0)//EOF
返回-1;
字符*结束;
errno=0;
长结果=strtol(缓冲区和结束区,10);
if(result<0 | | errno!=0)//既不允许出现错误也不允许出现负数
返回-1;
if(end==buffer)//没有可转换的内容
返回-1;
while(isspace(*end))
end++;
if(*end!='\0')//最后一位数字后没有空格
返回-1;
if(result>INT_MAX)//结果对于'INT'来说太大`
返回-1;
返回结果;
}
(如果您需要接受任何有效的
int
值,但要区分错误,则需要传入指向该函数的指针,并在出错时返回-1,成功时返回0,然后将安全结果分配给指针。)


是的,正确地做这项工作真是太麻烦了。是的,分析strtol()的结果就跟这一样棘手;你必须非常小心。(还有一种可能是我忘了检查可检测的错误情况。)不,我认为你不能用
scanf()
等做同样的工作;特别是,
scanf()
溢出时的行为未定义。

您正在处理
int
然后