Click here to Skip to main content
15,850,103 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C++
/*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;
 }
Posted
Updated 23-Dec-16 23:11pm
v2

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.

[EDIT]
Answering the comment
Quote:
I tried adding long long for NUM, but that didn't help. GetLongLong() is supposed to be a built-in function.

GetLongLong() is not a standard library function. I found this ([^]):
C#
/*
 * 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: ");
        }
    }
}

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:
C++
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]
 
Share this answer
 
v2
Comments
Member 12919791 23-Dec-16 20:29pm    
I tried adding long long for NUM, but that didn't help. GetLongLong() is supposed to be a built-in function. Thanks,
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900