0

I'm new to programming, and I'm giving the credit card problem (from Problem Set 1) a try. So far, I've run into two problems.

The first is that I'm not sure how I'm supposed to loop the output so that the program reprints "Number:" if the user input is not a numerical value. Is there a way to determine if the input is an integer or long-long or not?

The second is that the code compiles fine, but no matter what credit card number I enter, it always comes out with "INVALID". I've tried fixing it for quite some time now, so I'd really appreciate if a new set of eyes can look over it a bit.

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

int digits(long long number);
long long finder(int place, long long number);
bool checksum(long long number);

int main(void)
{

    printf("Number: ");
    long long number = get_long_long();

    checksum(number);

    if (checksum(number) == true)
    {
        if (digits(number) == 15)
        {
            printf("AMEX\n");
        }

        else if (digits(number == 13))
        {
            printf("VISA\n");
        }

        else if (digits(number) == 16)
        {
            if (finder(16, number) == 5)
            {
                printf("MASTERCARD\n");
            }

            else if (finder(16, number) == 3)
            {
                printf("VISA\n");
            }
        }
    }
        else
        {
            printf("INVALID\n");
        }
}

bool checksum(long long number)
{
        int partial = 0;

        for (int counter = 2; counter < digits(number); counter = counter + 2)
        {
            partial = partial + finder(counter, number);
        }

    if ((2 * partial) % 10 == 0)
    {
        return true;
    }

    else
    {
        return false;
    }
}

long long finder(int place, long long number)
{
    for (int i = 0; i < (place - 1); i++)
    {
        number = number/10;
    }

    return (number % 10);
}

int digits(long long number)
{
    int counter = 0;
    do
    {
        number = number/10;
        counter++;
    }
    while (number > 0);

    return counter;
}

1 Answer 1

1

your problem based on your function checksum. Take a look inside of the background description:

partial = partial + finder(counter, number);

your line is partially right. the part *finder(counter, number)" do not take of number higher 10.

And at least: You're missing followed step inside of your function ;-)

  1. Now let’s add that sum (27) to the sum of the digits that weren’t multiplied by 2:

best regards

Jens Fischer

2
  • Hey, thanks for your answer. I'm not quite sure what you mean. Why does "finder(counter, number)" not take a number higher than 10?
    – lithium123
    Commented Dec 19, 2017 at 19:10
  • Hi, if you getting a number higher 9 you have not to add the number by self. You have to both digits by them own... par example: if you have 12 after double.. you not use this value. You've to calculate instead 1 + 2. Hopefully this help you Commented Dec 20, 2017 at 21:20

You must log in to answer this question.

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