'How can I fix the error : Floating point exception (core dumped) in my program?
Several weeks ago, I started to do the CS50 course on EdX to learn a little bit about Computer Science then I started to code in C, so if the program that follows is not really well made, that’s normal. So basically, when I try to run my program, I get the error message : Floating point exception (core dumped). By looking on the Internet, I understood that it means that there is some expression dividing value by zero in my program, but I cannot figure out where the error comes from. So, please help. And thank you very much in advance for your response.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
//Prompt for input
long cardNum = get_long("Enter your card number : ");
//Calculate the checksum
int totalneedx2 = 0;
int total;
int needx2;
for (int n = 100; n < cardNum; n = n * 100)
{
needx2 = cardNum % n / n * 10;
if (needx2 >= 5)
{
int unit;
needx2 = needx2 * 2;
unit = needx2 % 10;
needx2 = needx2 / 10;
total = needx2 + unit;
}
else if (needx2 < 5)
{
needx2 = needx2 * 2;
total = needx2;
}
totalneedx2 = total + totalneedx2;
needx2 = 0;
}
int nox2;
int totalnox2 = 0;
for (int n = 10; n < cardNum; n = n * 100)
{
nox2 = cardNum % n / n * 10;
totalnox2 = totalnox2 + nox2;
nox2 = 0;
}
int totalChecksum = totalnox2 + totalneedx2;
if (totalChecksum % 10 == 0)
{
if (cardNum >= 1000000000000)
{
if (cardNum / 1000000000000 == 4)
{
printf("VISA\n");
}
else
{
printf("INVALID\n");
}
}
else if (cardNum >= 100000000000000)
{
if (cardNum / 10000000000000 == 34 || cardNum / 10000000000000 == 37)
{
printf("AMEX/n");
}
else
{
printf("INVALID\n");
}
}
else if (cardNum >= 1000000000000000)
{
if (cardNum / 100000000000000 == 51 || cardNum / 100000000000000 == 52 || cardNum / 100000000000000 == 53 || cardNum / 100000000000000 == 54 || cardNum / 100000000000000 == 55)
{
printf ("MASTERCARD\n");
}
else if (cardNum / 1000000000000000 == 4)
{
printf ("VISA\n");
}
else
{
printf("INVALID\n");
}
}
}
else
{
printf("INVALID\n");
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
