I spent so many hours and still can't figure the problem, any guidance would be appreciated.. I checked against all the paypal test numbers and got same response as the staff's execution, though I got a score of 4 in coding.. What's wrong?!
#include <cs50.h>
#include <stdio.h>
#include <ctype.h>
#include <math.h>
int main(void)
{
// get user’s input
long long number = get_long("Number: ");
// loop through number till second-to-last digit
// multiply each of the underlined digits by 2
int digit;
int length = 0;
int lastDigit;
int count = 0;
int sumMultiplied = 0;
int sumNonMultiplied = 0;
int all = 0;
int fstNum = 0;
int scndNum = 0;
bool Visa = false;
bool expressNum = false;
bool masterCard = false;
long long number1 = number;
long long number2 = number;
long long number3 = number;
while (number1 != 0)
{
number1 = number1 / 10;
length++;
}
lastDigit = number3 % 10;
while (number3 > 0) // stop when there's no more digits
{
// integer division by 10 removes the last digit, so you're ready to read the next one
number3 = number3 / 10;
// modulo 10 effectively takes the last (right-most) digit and ignores the others
digit = (int)(number3 % 10);
count++;
// get fst number from the left
if (count == length - 1)
{
fstNum = digit;
}
// get scnd number from the left
if (count == length - 2)
{
scndNum = digit;
}
if (count % 2 != 0)
{
digit = digit * 2;
sumMultiplied = sumMultiplied + digit / 10 + digit % 10;
}
if (count % 2 == 0)
{
sumNonMultiplied = sumNonMultiplied + digit / 10 + digit % 10;
}
}
if (fstNum == 4)
{
Visa = true;
}
else if ((fstNum == 3) && ((scndNum == 4) || (scndNum == 7)))
{
expressNum = true;
}
else if ((fstNum == 5) && ((scndNum >= 1) && (scndNum <= 5)))
{
masterCard = true;
}
all = lastDigit + sumMultiplied + sumNonMultiplied;
// check if Visa
if ((all % 10 == 0) && (Visa == true))
{
printf("VISA\n");
}
// check if American Express or AMEX
else if ((all % 10 == 0) && (expressNum == true))
{
printf("AMEX\n");
}
// check if Master Card
else if ((all % 10 == 0) && (masterCard == true))
{
printf("MASTERCARD\n");
}
// if non of the above then INVALID
else
{
printf("INVALID\n");
}
}