0

I ran compile this:

char get_credit_card_status(long long credit_card_number, char Luhn_validity)//, char credit_card_status)
{
    char credit_card_status = 0;
    if (((credit_card_number < 350000000000000) && (340000000000000 <=credit_card_number)) && (Luhn_validity=1))
    {
        credit_card_status = 1;
        printf("1 test\n");
    }
    else if ((5100000000000000<= credit_card_number) && (credit_card_number < 5500000000000000) && Luhn_validity)
    {
        credit_card_status = 2;
        printf("2 test\n");
    }
    else if ((((4000000000000<= credit_card_number)&&(credit_card_number<5000000000000)) || ((4000000000000000<=credit_card_number)&&(credit_card_number<5000000000000000))) && Luhn_validity)
    {
        credit_card_status= 3;
        printf("3 test\n");
    }
    return credit_card_status;
}

int length_ccn(long long credit_card_number)
{
    int length = 0;
    while (credit_card_number > 0)
    {
        length++;
        credit_card_number /= 10 ;
    }
    return length;
}

char Luhn_check(long long credit_card_number)
{
    int total_even_numbers = 0;
    int total_odd_numbers = 0;
    int even_number = 0;
    int odd_number = 0;
    int Luhn_sum = 0;
    char Luhn_validity = 0;
    long long check_digit_basis = credit_card_number;
    for(int i =1 ; check_digit_basis > 0 ; i++)
    {
        if ((i % 2) > 0)
        {
            odd_number = (check_digit_basis % 10);
            total_odd_numbers = total_odd_numbers + odd_number;
        }
        else
        {
            even_number = (check_digit_basis % 10);
            if(even_number >= 5)
            {
                total_even_numbers = total_even_numbers + ((( 2 * even_number) % 10) + 1 );
            }
            else
            {
                total_even_numbers = total_even_numbers + (2 * even_number);
            }
        }
        check_digit_basis = (check_digit_basis / 10);

    }
    Luhn_sum = total_even_numbers + total_odd_numbers;
    if ((Luhn_sum % 10 ) == 0 )
    {
        Luhn_validity = 1;
    }
    else
    {
        Luhn_validity = 0;
        printf("%c for Luhn_validity\n", Luhn_validity);
    }
    printf("%d for Luhn_validity\n", Luhn_validity);
    return Luhn_validity;
}



int main(void)
{
    printf("Provide your credit card number:\n");
    long long credit_card_number = get_long_long();
    int length = length_ccn(credit_card_number);
    printf("%i for length\n", length);
    char Luhn_validity = Luhn_check(credit_card_number);
    printf("%c returned from line 119\n", Luhn_validity);
    char credit_card_status;
    credit_card_status = get_credit_card_status(credit_card_number, Luhn_validity);
    switch (credit_card_status)
    {
        case 1:
        printf("American Express\n");
        break;

        case 2:
        printf("MasterCard\n");
        break;

        case 3:
        printf("Visa\n");
        break;

        default:
        printf("INVALID\n");
    }
}

I got:

15 for length
1 for Luhn_validity
 returned from line 119

The value of Length is returned to the main function while Luhn_validity isn't. Why?

I cannot make another post because someone posted an incomplete answer only answering to what I already began to implement (see the post before its edits and you'll see that the boolean problem was being corrected and wasn't in the post's title).

Can you help me?

Thanks in advance.

2 Answers 2

1

Yes, you can return a char, if you do it right.

The code above is designed to return a char, but you are assigning integers (1, 2, 3) to credit_card_status. A char also behaves like a signed int. Because of this, the code is actually taking those numbers and treating them like ASCII values. So, it means that the function interprets those digits as ASCII values and returns the non-printable characters associated with them. (Try googling ASCII TABLE and study it.)

If you actually wanted to return a char, you should assign an actual char to credit_card_status, such as '1', '2', '3', 'M', 'V', etc. The secret is to enclose the char in single quotes so that the code actually recognizes it as the speecific char and not as an ASCII code.

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

4
  • In addition, char on our system is signed by default (hit this one in recover, where I initially used a char[512]). And one can print char as int by using a typecast, like printf("%d\n", (int)your_char);. I'd use an int instead of char, shouldn't make much of a difference in terms of efficiency (unless you store a ton of those values at the same time).
    – Blauelf
    Commented May 17, 2018 at 7:52
  • I don't see where I assign int to credit_card_status. Above, you consider about 1,2,3 as int and below you consider them as char...
    – Nocxy
    Commented May 17, 2018 at 10:39
  • This assigns an int to credit_card status: credit_card_status = 1; By not enclosing the 1 in single quotes, it assigns the integer value 1. If the 1 were enclosed in single quotes, the character 1 , which is also the ASCII integer value of 49, would have been assigned to credit_card_status. Remember, char typess also behave the same as one byte signed integers by using the ASCII value of the char as an integer - not by using the char itself. For example, 'c' - 2 = 99 - 2 = 97 = 'a' Perhaps you should review the class material on chars.
    – Cliff B
    Commented May 17, 2018 at 21:50
  • Also, to give more examples, 1 + 5 = 6 while '1' + 5 = 54 = '6' The difference is that the first example is the simple addition of two integers. The second example, where 1 and 6 are in single quotes, shows what happens when you add 5 to the ASCII value of the character '1' , resulting in the integer value of 54, which represents the character of '6'. To take it further,'5' + 50 = 53 + 50 = 103 = 'g'
    – Cliff B
    Commented May 18, 2018 at 1:32
-1

For people facing the same problem: it seems like you can't return a char. Good to know.

You must log in to answer this question.

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