Hitchiker-V
Hitchiker-V

Reputation: 9

How to solve a runtime error problem like " signed integer overflow: 2147483644 + 4 cannot be represented in type 'int' "?

I even tried to use long int in place of int but then the compiler takes too much time to execute and still doesn't give any output.

I'm using CS50's IDE.

Here's my code that's causing the problem:

int sum_alt = 0;
int sum_rest = 0;
int total = 0;

for(int i = 1; i < count; i=+2)
{
    int temp = number[i] * 2;
    sum_alt = sum_alt + temp;
}

for(int i = 0; i <= count; i=+2)
{
    int temp = number[i];
    sum_rest = sum_rest + temp;
}

total = sum_rest + sum_alt;

How to resolve this problem?

Here's the full code:

#include <stdio.h>
#include <cs50.h>

int main(void)
{
long long Cardnumber;

//Typos correction + User input
do
{
    Cardnumber = get_long_long("Number: ");
}
while(Cardnumber < 0);

int count = 0;
long long counter = Cardnumber;

//To count the number of digits
while(counter != 0)
{
    counter = counter / 10;
    count++;
}

//Declaring an array for cardnumber
printf("%i", count);
int number[count];

if((count != 13) && (count != 15) && (count != 16))
{
    printf("INVALID\n");
}

for(int access = 0; access < count; access++)
{
    number[access] = Cardnumber % 10;
    Cardnumber = Cardnumber / 10;
}

int originalnumber[count];

for(int i = 0; i < count; i++)
{
    originalnumber[i] = number[i];
}

long long int sum_alt = 0;
long long int sum_rest = 0;
long long int total = 0;

for(int i = 1; i < count; i=+2)
{
    int temp = number[i] * 2;
    sum_alt = sum_alt + temp;
}

for(int i = 0; i < count; i=+2)
{
    int temp = number[i];
    sum_rest = sum_rest + temp;
}

total = sum_rest + sum_alt;

if(total % 10 == 0)
{
    if(((count == 13) || (count == 16)))
    {
        if((originalnumber[12] == 4) || originalnumber[15] == 4)
        {
            printf("VISA\n");
        }
        else if(originalnumber[15]== 5)
        {
            if(originalnumber[14] == 1 || originalnumber[14]== 2 || originalnumber[14] == 3 || originalnumber[14] == 4 || originalnumber[14] == 5)
            {
                printf("MASTERCARD\n");
            }
        }

        else
        {
            printf("INVALID\n");
        }
    }
    else if (count == 15)
    {
        if(originalnumber[15] == 3)
        {
            if(originalnumber[15] == 4 || originalnumber[15] == 7)
            {
                printf("AMERICAN EXPRESS\n");
            }
            else
            {
                printf("INVALID\n");
            }
        }
        else
        {
            printf("INVALID\n");
        }
    }
}

else
{
    printf("INVALID\n");
}   

}

PS. I don't get the reason why CS50's IDE is taking so much time for computation.

Upvotes: 0

Views: 3416

Answers (2)

chmike
chmike

Reputation: 22174

The following code gets a credit card number from the user, check its length and check sum, and determine the issuer.

The program can be tested with credit card numbers obtained from https://ccardgenerator.com/.

The properties of this code is that

  • it doesn't input an integer number that may indeed overflow
  • it processes a sequence of characters
  • the Luhn algorithm is properly implemented (the digits of number[i]*2 must be summed)
  • I refined the tests to recognize the credit card issuer (base on the first 4 digits); the original code had a lot of errors in it
#include <stdio.h>
#include <ctype.h>

int main()
{
    char buf[32];
    printf("enter credit card number: ");
    fgets(buf, sizeof(buf), stdin);

    int len = 0;
    while(isdigit(buf[len]))
        len++;
    if(len < 13 || len > 16) {
        printf("INVALID\n");
        return 1;
    }

    // verify credit card number check sum
    int checkSum = 0;
    for(int i = 0; i < len; i++) {
        int d = buf[len-i-1] - '0'; // get digit from right most to left most 
        if((i&1) == 0)
            checkSum += d;
        else
            checkSum += (d*2)%10 + (d*2)/10;
    }
    if(checkSum%10 != 0) {
        printf("INVALID\n");
        return 1;
    }

    // determine issuer (see https://www.regular-expressions.info/creditcard.html)
    int start = (buf[0]-'0')*1000 + (buf[1]-'0')*100 + (buf[2]-'0')*10 + buf[3]-'0';
    if((len == 13 || len == 16) && start/1000 == 4) {
        printf("VISA\n");
    } else if(len == 16 && ((start >= 5100 && start < 5600) || (start >= 2221 && start <= 2720))) {
        printf("MASTERCARD\n");
    } else if(len == 15 && ((start >= 3400 && start < 3500) || (start >= 3700 && start < 3800))) {
        printf("AMERICAN EXPRESS\n");
    } else if(len == 14 && ((start >= 3000 && start < 3060) || start/100 == 36 || start/100 == 38)) {
        printf("DINERS CLUB\n");
    } else if(len == 16 && (start == 6011 || start/100 == 65)) {
        printf("DISCOVER\n");
    } else if((len == 15 && (start == 2131 || start == 1800)) || (len == 16 && start/100 == 35)) {
        printf("JCB\n");
    } else {
        printf("UNKNOWN\n");
    }

    return 0;
}

Upvotes: 1

Lectan
Lectan

Reputation: 294

This is because 2147483644 + 4 = 2147483648 which is more than 2147483647 (which is signed int32 limit). And 64-bit integer type is long long int, AFAIK.

So it should be:

long long int sum_alt = 0;
long long int sum_rest = 0;
long long int total = 0;

for(int i = 1; i < count; i=+2)
{
    int temp = number[i] * 2;
    sum_alt = sum_alt + temp;
}

for(int i = 0; i <= count; i=+2)
{
    int temp = number[i];
    sum_rest = sum_rest + temp;
}

total = sum_rest + sum_alt;

P.S. Also, the variable "count" is not defined according to this code, so there might be a problem with it.

Upvotes: 1

Related Questions