C代码错误:我不确定为什么会出现此错误 [英] C code error: I am not sure why I get this error

查看:79
本文介绍了C代码错误:我不确定为什么会出现此错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这些代码行中,我想找到一个信用卡号长度,NUM。我不知道为什么会出现这个错误。



终端输入和输出:



输入数字:`123456789重试:12345678912(/ home / ubuntu / workspace / pset1 / credit + 0x42bf01):运行时错误:值1e + 11超出了'int'类型的可表示值范围(/ home / ubuntu / workspace / pset1 / credit + 0x42bf01):运行时错误:值1e + 10超出类型'int'的可表示值的范围INVALID



~ / workspace / pset1 / $ ./ credit输入数字:123456789123456(/ home / ubuntu / workspace / pset1 / credit + 0x42bf01):运行时错误:值1e + 11超出了'int'类型的可表示值范围(/ home / ubuntu / workspace / pset1 / credit + 0x42bf01):运行时错误:值1e + 13超出类型'int'的可表示值范围(/ home / ubuntu / workspace / pset1 / credit + 0x42bf01):运行时错误:值1e + 15超出范围'int'类型的可表示值



(/ home / ubuntu / workspace / pset1 / credit + 0x42b f01):运行时错误:值1e + 10超出类型'int'的可表示值范围(/ home / ubuntu / workspace / pset1 / credit + 0x42bf01):运行时错误:值1e + 12超出可表示的范围类型'int'的值(/ home / ubuntu / workspace / pset1 / credit + 0x42bf01):运行时错误:值1e + 14超出类型'int'的可表示值范围内INVALID



我尝试过:



这是我的代码:



In these lines of code, I want to find the "length" a credit card number, "NUM". I can't know why I get this error.

Terminal input and output:

Enter number: `123456789 Retry: 12345678912 (/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+11 is outside the range of representable values of type 'int' (/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+10 is outside the range of representable values of type 'int' INVALID

~/workspace/pset1/ $ ./credit Enter number: 123456789123456 (/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+11 is outside the range of representable values of type 'int' (/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+13 is outside the range of representable values of type 'int' (/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+15 is outside the range of representable values of type 'int'

(/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+10 is outside the range of representable values of type 'int' (/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+12 is outside the range of representable values of type 'int' (/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+14 is outside the range of representable values of type 'int' INVALID

What I have tried:

Here is my code:

/*PROMPT USER FOR NUMBER*/
 printf("Enter number: \n");

 /*STORE NUMBER AS A LONG NUMBER*/
 long NUM = GetLongLong();

 /*VARIABLES FOR WHILE LOOP*/
 long long range1 = 9;
 long long length = 1;

 /*FIND NUM'S LENGTH*/
 while
 (NUM > range1)

 {
    range1 = range1 * 10LL + 9;
    length += 1;
 }

推荐答案

./ credit输入数字:123456789123456(/ home / ubuntu / workspace / pset1 / credit + 0x42bf01) :运行时错误:值1e + 11超出了'int'类型的可表示值范围(/ home / ubuntu / workspace / pset1 / credit + 0x42bf01):运行时错误:值1e + 13超出了可表示值的范围type'int'(/ home / ubuntu / workspace / pset1 / credit + 0x42bf01):运行时错误:值1e + 15超出'int'类型的可表示值范围



(/ home / ubuntu / workspace / pset1 / credit + 0x42bf01):运行时错误:值1e + 10超出了'int'类型的可表示值范围(/ home / ubuntu / workspace / pset1 / credit + 0x42bf01):运行时错误:值1e + 12超出类型'int'的可表示值范围(/ home / ubuntu / workspace / pset1 / credit + 0x42bf01):运行时错误:值1e + 14超出可表示的范围'int'类型的值无效



我有什么试过:



这是我的代码:



./credit Enter number: 123456789123456 (/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+11 is outside the range of representable values of type 'int' (/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+13 is outside the range of representable values of type 'int' (/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+15 is outside the range of representable values of type 'int'

(/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+10 is outside the range of representable values of type 'int' (/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+12 is outside the range of representable values of type 'int' (/home/ubuntu/workspace/pset1/credit+0x42bf01): runtime error: value 1e+14 is outside the range of representable values of type 'int' INVALID

What I have tried:

Here is my code:

/*PROMPT USER FOR NUMBER*/
 printf("Enter number: \n");

 /*STORE NUMBER AS A LONG NUMBER*/
 long NUM = GetLongLong();

 /*VARIABLES FOR WHILE LOOP*/
 long long range1 = 9;
 long long length = 1;

 /*FIND NUM'S LENGTH*/
 while
 (NUM > range1)

 {
    range1 = range1 * 10LL + 9;
    length += 1;
 }


代码看起来基本上没问题。



假设 GetLongLong()返回 long long ,输入大于 LONG_MAX时可能会出错(2147483647)转换为 long 时。但是,该行应该有一个编译器警告,因为没有使用转换。



如果是这样,这可以通过使用 long long来解决也适用于NUM。

附注:不要在C / C ++中使用所有大写变量名。这些通常仅用于 #define s。



错误也可能出现在<$ c $中c> GetLongLong()你没有显示代码的函数。





回答注释
The code looks basically OK.

Assuming that GetLongLong() returns a long long, the error may occur when the input is larger than LONG_MAX (2147483647) when converted to long. However, there should be a compiler warning on that line because no casting is used.

If so, this can be solved by using long long for NUM too.
Side note: Don't use all upper case variable names with C/C++. These are usually used only with #defines.

The error may also occur in the GetLongLong() function for which you did not show the code.


Answering the comment
引用:

我尝试为NUM添加long long,但这没有帮助。 GetLongLong()应该是一个内置函数。

I tried adding long long for NUM, but that didn't help. GetLongLong() is supposed to be a built-in function.



GetLongLong()不是标准的库函数。我找到了这个( [ ^ ]):


GetLongLong() is not a standard library function. I found this ([^]):

/*
 * Reads a line of text from standard input and returns an equivalent
 * long long in the range [-2^63 + 1, 2^63 - 2], if possible; if text
 * does not represent such a long long, user is prompted to retry.
 * Leading and trailing whitespace is ignored.  For simplicity, overflow
 * is not detected.  If line can't be read, returns LLONG_MAX.
 */

long long
GetLongLong(void)
{
    // try to get a long long from user
    while (true)
    {
        // get line of text, returning LLONG_MAX on failure
        string line = GetString();
        if (line == NULL)
            return LLONG_MAX;

        // return a long long if only a long long (possibly with
        // leading and/or trailing whitespace) was provided
        long long n; char c;
        if (sscanf(line, " %lld %c", &n, &c) == 1)
        {
            free(line);
            return n;
        }
        else
        {
            free(line);
            printf("Retry: ");
        }
    }
}



它解释了为什么在输出中打印重试。我现在可以假设使用的库的 sscanf 实现无法扫描64位整数。



但是,输入123456789应该有效,因为它小于 LONG_MAX



你可以尝试使用 strtoll - C ++参考 [ ^ ]:


It explains why "Retry" is printed in the output. All I can assume now is that the sscanf implementation of the used library is failing to scan 64-bit integers.

However, the input "123456789" should work because it is less than LONG_MAX.

You may try to use strtoll - C++ Reference[^] instead:

long long GetLongLong()
{
    char *endptr;
    string line = GetString();
    long long num = strtoll(line, &endptr, 10);
    // Conversion stops at first char that is not a digit.
    // May inspect *endptr to detect this.
    // Zero if not a number.
    // LLONG_MAX or LLONG_MIN if out of range (errno is set to ERANGE).
    free(line);
    return num;
}

[/ EDIT]


不要这样做;信用卡号码不是单一值。将数字作为字符串读取,然后您需要做的就是获取字符串中的位数。
Don't do it like that; a credit card number is not a single value. Read the digits as a string and then all you need to do is get the number of digits in the string.


这篇关于C代码错误:我不确定为什么会出现此错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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