0

I was almost finishing my code when I got stuck with a strange problem. I've asked a question a few hours ago and someone answered, so I manage to fix that issue. But unfortunelly I got stuck in another :/

The issue happens when I try to get the brand to show, It does not show. Apparently there is a problem with the type of my variables, but I couldn't manage to fix It. I think I have to get rid of the decimals in my integer variables. Thanks for the help, have a good night. :D

The code:

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

long number;
int count;
long temp_n;
long temp_b;
long temp_c;
long temp_d;
long temp_e;
int digits;
int sum_1;
int sum_2;

int main(void)
{
    do
    {
        number = get_long("Number: "); //Ask the CC number.
    }
    while(number < 0); //If the user answer a negative value, ask again.

    temp_n = number;
    temp_b = number;
    temp_c = number;
    temp_d = number;
    temp_e = number;

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

    sum_1 = 0;
    sum_2 = 0;
    int temp_a = temp_n;
    int temp_b = temp_n;
    int now_1 = 0;

    for (digits = 1; digits <= count; digits++)
    {

        if (digits % 2 == 0)
        {
            now_1 = now_1 + (2 * (temp_n % 10));

            if (now_1 > 9)
            {
                int now_2 = now_1 % 10;
                int now_3 = now_1 / 10;
                sum_1 = sum_1 + now_2 + now_3;
                temp_n /= 10;
                now_1 = 0;
            }
            else
            {
                sum_1 = sum_1 + now_1;
                temp_n /= 10;
                now_1 = 0;
            }
        }
        else
        {
            sum_2 = sum_2 + (temp_n % 10);
            temp_n /= 10;
        }

    }

    int sum = sum_1 + sum_2;
    temp_b /= (10 ^ 14); //Getting the fist two digits of a number with 16 characters.
    temp_c /= (10 ^ 13); //Getting the fist two digits of a number with 15 characters.
    temp_d /= (10 ^ 12); //Getting the fist digit of a number with 13 characters.
    temp_e /= (10 ^ 15); //Getting the fist digit of a number with 16 characters.

    if (sum % 10 == 0) //"If the card is valid".
    {
        if (count == 15 && (temp_c == 34 || temp_c == 37)) //Checking if it's AmericanExpress.
        {
            printf("AMEX\n");
        }
        else if (count == 16 && temp_b >= 51 && temp_b <= 56) //Checking if it's MasterCard.
        {
            printf("MASTERCARD\n");
        }
        else if ((count == 13 && temp_d == 4) || (count == 16 && temp_e == 4)) //Checking if it's VISA.
        {
            printf("VISA\n");
        }
    }
    else
    {
        printf("INVALID\n"); //"If the card is invalid".
    }
}

1 Answer 1

1

Having problems with this?

temp_b /= (10 ^ 14);

That would be because the ^ operator is not an exponent operator as it is in mathematics. It's the exclusive or operator. You might try a call to pow().

As for float issues, I didn't see any floats in the code at all.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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