本文介绍了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;
 }

推荐答案

/*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;
 }


引用:

我尝试为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



你可以尝试使用]:


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]



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

08-15 09:54