Hy CS50
I working on the Credit program for pset1 right now, but somehow it seems like my output is still a bit messed up... and i just don´t get where the issue is! Here the output from check50, and my code further below.
:) credit.c exists.
:) credit.c compiles.
:) identifies 378282246310005 as AMEX
:) identifies 371449635398431 as AMEX
:) identifies 5555555555554444 as MASTERCARD
:( identifies 5105105105105100 as MASTERCARD
expected "MASTERCARD\n", not "INVALID\n"
:( identifies 4111111111111111 as VISA
expected "VISA\n", not "INVALID\n"
:( identifies 4012888888881881 as VISA
expected "VISA\n", not "INVALID\n"
:) identifies 1234567890 as INVALID
:) rejects a non-numeric input of "foo"
:) rejects a non-numeric input of ""
Here the Code :
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int numberLength(long long ccNumber)
{
int counter = 0;
while (ccNumber > 0)
{
counter++;
ccNumber = ccNumber / 10;
}
return counter;
}
bool lengthCheck(int counting)
{
if (counting == 13)
{
return true;
}
else if (counting == 15)
{
return true;
}
else if (counting == 16)
{
return true;
}
else
{
return false;
}
}
int main()
{
long long ccNumber = 0;
int counter = 0;
int lengthChecked = 0;
printf("Please enter your Credit Card Number: ");
ccNumber = get_long_long();
counter = numberLength(ccNumber);
lengthChecked = lengthCheck(counter);
int ccArray[counter];
int iterator = counter - 1;
while (iterator >= 0)
{
int digit = ccNumber % 10;
ccArray[iterator] = digit;
ccNumber = ccNumber / 10;
iterator--;
}
int firstSum = 0;
int secondSum = 0;
int doubleCheck = 0;
for (int i = 0; i < counter; i++)
{
if (i % 2 == 0)
{
secondSum = secondSum + ccArray[i];
}
if (i % 2 == 1)
{
doubleCheck = ccArray[i] * 2;
if (doubleCheck >= 10)
{
firstSum = firstSum + (doubleCheck % 10);
firstSum = firstSum + (doubleCheck / 10);
}
else
{
firstSum += doubleCheck;
}
}
}
int totalChecksum = firstSum + secondSum;
if (totalChecksum % 10 == 0 && lengthChecked == 1)
{
if (ccArray[0] == 3)
{
if (ccArray[1] == 4 || ccArray[1] == 7)
printf("AMEX\n");
}
else if (ccArray[0] == 4)
{
printf("VISA\n");
}
else if (ccArray[0] == 5)
{
if (ccArray[1] == 1 || ccArray[1] == 2 || ccArray[1] == 3 || ccArray[1] == 4 || ccArray[1] == 5)
{
printf("MASTERCARD\n");
}
}
}
else
{
printf("INVALID\n");
}
return 0;
}