0

I tried to compile this:

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

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



// Provides the length of credit card number
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;
    printf("Line 43 worked\n");
    int odd_number = 0;
    int Luhn_sum = 0;
    //char Luhn_check = 0;
    char Luhn_validity = 0;
    printf("line 49 executed\n");
    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;
    printf("%i for Luhn_sum", Luhn_sum);
    if ((Luhn_sum % 10 ) == 0 )
    {
        Luhn_validity = 1;
        printf("%d for \n", Luhn_validity);
    }
    else
    {
        Luhn_validity = 0;
        printf("%d for\n", Luhn_validity);
    }
    //printf("%c for Luhn test", 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\n", length);
    char Luhn_validity = Luhn_check(credit_card_number);
    char credit_card_status;
    credit_card_status = get_credit_card_status(credit_card_number, Luhn_check);
    printf("credit_card_status");
    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:

credit-beta.c:23:9: error: incompatible pointer to integer conversion returning 'char (long long, char)' from a function with result type 'char'
      [-Werror,-Wint-conversion]
        return get_credit_card_status;
               ^~~~~~~~~~~~~~~~~~~~~~
credit-beta.c:8:64: error: unused parameter 'Luhn_check' [-Werror,-Wunused-parameter]
char get_credit_card_status(long long credit_card_number, char Luhn_check)//, char credit_card_status)
                                                               ^
credit-beta.c:125:66: error: incompatible pointer to integer conversion passing 'char (long long)' to parameter of type 'char'
      [-Werror,-Wint-conversion]
        credit_card_status = get_credit_card_status(credit_card_number, Luhn_check);
                                                                        ^~~~~~~~~~
credit-beta.c:8:64: note: passing argument to parameter 'Luhn_check' here
char get_credit_card_status(long long credit_card_number, char Luhn_check)//, char credit_card_status)
                                                               ^
credit-beta.c:141:11: error: label at end of compound statement: expected statement
                default:
                        ^
                         ;
4 errors generated.
make: *** [credit-beta] Error 1

I had more errors and tried to skim them one by one but I cannot figure out what I did wrong this time.

Everything was alright until I tried to implement credit_card_status.

Can you help me?

Thanks in advance.

0

Do not use same name for a function and a variable get_credit_card_status and Luhn_check. Compiler won't be able to understand which one you meant.

9
  • The thing is that I need this argument for the function to take Luhn_check returned by the other one so that it can use it. How can I do it otherwise?
    – Nocxy
    May 16 '18 at 12:55
  • Give that variable another name!
    – Blauelf
    May 16 '18 at 13:14
  • At which moment? I would like to get an exemple like "change #include<cs50.h> by x on that line" because all you answer me is "change" and I'm still wondering which one(s) I should change. Btw, how can I get Luhn-check involved in a function without calling it?
    – Nocxy
    May 16 '18 at 13:40
  • Inside your function Luhn_check, you declare a variable of name Luhn_check. Those names should not be the same. Change at least one of them to something else.
    – Blauelf
    May 16 '18 at 13:43
  • I still don't understand why a function named x should return a value y but now my concern is about the algo since it returns bad values.
    – Nocxy
    May 16 '18 at 14:19

You must log in to answer this question.

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