-1

Well I decided to make the credit.c program, and I did, I started debugging and so until it compiled. But it always outputs INVALID and I can´t find out why, so this is my code:

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

int main (void)
{
    //Initialize a longlong that will hold our creditcard number.
    long long creditcardnumber;

    //Ask the user for the creditcard number, and make sure it is positive.
    do
    {
        printf("Insert your creditcard number, please\n");
        printf("Just digits, thanks\n");
        creditcardnumber = GetLongLong();
    }
    while (creditcardnumber < 0);

    //We know the heighst number possible for any creditcard, if the input number of
    //bigger than the max range, we know this number is invalid
    if (creditcardnumber > 5599999999999999)
    {
        printf("INVALID\n");
    }
    //Start counting the digits, with a loop.
    int digitscount = 0;

    while (creditcardnumber > 1)
    {
        creditcardnumber = creditcardnumber/10;
        digitscount++;
    }

    //We know the length companies use, so if it is too short or too long, we
    //know the number is invalid.
    if ((digitscount != 15) && (digitscount != 16) && (digitscount != 13))
    {
        printf("INVALID\n");
    }

    //We initialize an array and with modulo we put each number in the right
    //place.
    int number[digitscount];
    for (int i = digitscount; i > 0; i--)
    {
        number[i] = creditcardnumber % 10;
        creditcardnumber = creditcardnumber / 10;
    }

    //We made 2 arrays out of 1, so that we can leave 1 array with the original
    //number and the other will have the second-to-last digits multiplied by two
    int realnumber[digitscount];
    for (int i = 1; i < digitscount; i++)
    {
        realnumber[i] = number[i];
    }

    int multipliednumber[digitscount];
    for (int i = 1; i < digitscount; i+=2)
    {
        multipliednumber[i] = number[i] * 2;
    }

    multipliednumber[2|4|6|8|10|12|14|16] =  number[2|4|6|8|10|12|14|16] * 2;

    //We need to sum the digits and not the products, so we substract 10 from
    //all the numbers that are greater than 10 and count how many times we do this
    //to represent the ones, that would be left.
    int tenscount = 0;

    if (multipliednumber[2|4|6|8|10|12|14|16] >= 10)
    {
        multipliednumber[2|4|6|8|10|12|14|16] = multipliednumber[2|4|6|8|10|12|14|16] - 10;
        tenscount++;
    }

    //We sum the multiplied digits + the tenscount
    int multiplieddigitssum = multipliednumber[2+4+6+8+10+12+14+16] + tenscount;
    int sumdigitstotal = multiplieddigitssum + realnumber[1+3+5+79+11+13+15];

    if (sumdigitstotal % 10 == 0)
    {
        if (digitscount == 15 && (realnumber[1] == 3) && (realnumber[2] == 4|realnumber[2] == 7))
        {
            printf("AMEX\n");
        }

        else
            if (digitscount == 16 && (realnumber[1] == 5) && (realnumber[2] == 1 |realnumber[2] == 2|realnumber[2] ==  3|realnumber[2] == 4|realnumber[2] == 5))
            {
                printf("MASTERCARD\n");
            }

            else
                if ((digitscount == 13|digitscount == 16) && (realnumber[1] == 4))
                {
                    printf("VISA\n");
                }

                else
                {
                    printf("INVALID\n");
                }
            }
            else
                printf("INVALID\n");


            return 0;
        }

Thanks in advance

1

You have multiple syntax mistakes. For example:

multipliednumber[2|4|6|8|10|12|14|16] =  number[2|4|6|8|10|12|14|16] * 2;

is not a valid expression in C. What exactly are you trying to achieve?

Also if you have multiple expressions in an if block, they should be enclosed in curly braces {}. So the following block will not execute as you intend.

else
    if (digitscount == 16 && (realnumber[1] == 5) && (realnumber[2] == 1 |realnumber[2] == 2|realnumber[2] ==  3|realnumber[2] == 4|realnumber[2] == 5))
    {
        printf("MASTERCARD\n");
    }

    else
        if ((digitscount == 13|digitscount == 16) && (realnumber[1] == 4))
        {
            printf("VISA\n");
        }

        else
        {
            printf("INVALID\n");
        }
    }
    else
        printf("INVALID\n");

I would suggest you watch the lectures and understand the basic concepts of C before diving into hacker psets and more difficult problems.

You must log in to answer this question.

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