0

i have problem with Credit (pset1), my problem is not about the algorithm but about something wired , the output of subtract and multiplication is not right at all
first of all i get a valid input from the user and store it in long double variable.then, then i got another 2 long double variables , one will be equal to input subtracted by 100 , and the other will be equal to the second variable but after applying truncl() function to it to get rid of decimal number.then, i subtract 2nd and 3rd variables to get the last 2 digits and multiply it by 100 to be able to have those 2 digits outside of number like this.

    long double creditNumber;
    long double checker1, checker2;
    do
    {
        creditNumber = get_long_long("Enter Your Credit card number: ");
    }while(!creditNumber);
    while(creditNumber / 100 >= 10)
    {
        checker1 = creditNumber / 100;  //divide input by 100 to separate last 2 digits   
        printf("%Lf\n", checker1);
        checker2 = truncl(checker1);     //convert number after division to decimal
        printf("%Lf\n", checker2);
        checker3 = (checker1 - checker2) * 100; //subtract both number to get just last 2 digits
        printf("%f\n", checker3);
        x = checker3 ;             //double 2 digits by 100 and assignt it to integerif(checker3 < 10)

my problem that , suppose that the input is 5555555555554444 so first of all checker1 = 55555555555544.44 but this doesnt happen, the output of checker 1 is 55555555555544.439999 and that's ruined my whole code because every number i try to check will be decreased by one, so how to solve this . and this is my whole code if you have any comment or correction on my algorithm.

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

int main(void)
{
    int x,y,z = 0;
    int counter = 0;
    int numberChecker = 0;
    float checker3;
    long double creditNumber;
    long double checker1, checker2;
    do
    {
        creditNumber = get_long_long("Enter Your Credit card number: ");
    }while(!creditNumber);
    while(creditNumber / 100 >= 10)
    {
        checker1 = creditNumber / 100;  //divide input by 100 to separate last 2 digits   
        printf("%Lf\n", checker1);
        checker2 = truncl(checker1);     //convert number after division to decimal
        printf("%Lf\n", checker2);
        checker3 = (checker1 - checker2) * 100; //subtract both number to get just last 2 digits
        printf("%f\n", checker3);
        x = checker3 ;             //double 2 digits by 100 and assignt it to integerif(checker3 < 10)
        printf("%i\n", x);
        if(x < 10)
        {
            numberChecker += x;
            printf("%i\n", numberChecker);
        }
        else
        {
            y = x % 10 ;                    //that will get just last digit
            printf("%i\n", y);
            z = x / 10 ;                    //that will get second to last digit
            printf("%i\n", z);
            numberChecker += (z*2) + y;     //the mathematic operation needed to check validation
                                            //by double second to last digit and add it to last digit.
            printf("%i\n", numberChecker);
        }
        counter += 2;                   //counting how many digits have been eliminated
        printf("%i\n", counter);
        creditNumber = creditNumber / 100;
        printf("%Lf\n", creditNumber);
    }
    if(creditNumber < 100)
    {
        if(counter != 14)
        {
            printf("INVALID\n");
        }
        else
        {
            x = creditNumber;
            y = x % 10;
            z = creditNumber / 10;
            numberChecker += (z*2) + y;
            if(numberChecker % 10 != 0)
            {
                printf("Invalid\n");
            }
            else
            {
                if(creditNumber == 51 || creditNumber == 52 || creditNumber == 53 || creditNumber == 54 ||
                   creditNumber == 55 )
                {
                    printf("MASTERCARD\n");
                }
                else
                {
                    x = creditNumber / 10 ;
                    if (x == 4)
                    {
                        printf("VISA\n");
                    }
                    else
                    {
                        printf("Invalid\n");
                    }
                }
            }
        }
    }
    else if(creditNumber > 100)
    {
        if(counter == 10)
        {
            x = creditNumber;
            y = x % 10;
            numberChecker += y;
            x = x / 10;
            y = x % 10;
            numberChecker += (y*2);
            x = x / 10;
            numberChecker += x;
            if(numberChecker % 10 != 0)
            {
                printf("INVALId\n");
            }
            else
            {
                if(x == 4)
                {
                    printf("VISA\n");
                }
                else
                {
                    printf("INVALiD\n");
                }
            }
        }
        else if(counter == 12)
        {
            x = creditNumber;
            y = x % 10;
            numberChecker += y;
            x = x / 10;
            y = x % 10;
            numberChecker += (y*2);
            x = x / 10;
            numberChecker += x;
            if(numberChecker % 10 != 0)
            {
                printf("INVAlID\n");
            }
            else
            {
                if(x == 34 || x == 37)
                {
                    printf("AMEX\n");
                }
                else
                {
                    printf("INVaLID\n");
                }
            }    
        }
        else
        {
            printf("Invalid\n");
        }
    }
}

and i cant use round here because if the number besides the decimal point is equal or greater than 5 , so the output will be increased by 1 than actual number.

1

Don't use floating point. There's no need.

You could just do x = creditNumber % 100; to get the last two digits.

BTW, your code is overly complex. Try to get some information, like checksum, number of digits, and first two digits, and then, after obtaining all of them, go into the INVALID/MASTERCARD/VISA/AMEX decision tree. This avoids repeating much of the code.

And I think your maths is a bit flawed. When you are meant to add the digits of the doubled value (7*2 should result in 1+4=5), you seem to ignore that detail. Also, no need to handle x < 10 separately.

1
  • it is my fault, i didn't read instructions carefully, i have solved this now , thanks
    – DrJempo
    Jun 7 '18 at 12:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .