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;
}