#include <stdio.h>
#include <cs50.h>
int main(void)
{
long creditcardno;
do
{
creditcardno = get_long("enter");
if (creditcardno < 999999999999 || creditcardno > 9999999999999999)
printf ("INVALID\n");
}
while (creditcardno < 999999999999 || creditcardno > 9999999999999999);
int i = 0; //will count no. of digits
long cc = creditcardno; //cc will help compute no. of digits
while (cc > 0)
{
cc = cc / 10;
i++;
}
printf ("noofdigits %i", i);
if (i == 14)
{
printf("INVALID2");//if a 14 digit no. entered, mark it as invalid.
return 0; //if 14 digits entered, program terminated
}
int mod1 = 0; //will store modulus of entered credit card no. from the last alternatively
int mod2 = 0; //will store modulus of entered credit card no. from the second last
alternatively
int mod2doubled = 0;//will store mod2 doubled
int mod2d1 = 0; //will store modulus of mod2 after being doubled
int mod2d2 = 0; //will store remainder of mod2 after being doubled
int sum1 = 0; //will store sum of results from the operations of the entered credit card no.
from last alternatively
int sum2 = 0; //will store sum of results for the operations of second last numbers
alternatively of entered credit card no.
int sum = 0; //will store desired no. after performed operations on sum1 and sum2
int sumofdoubled = 0;
long tt = creditcardno; //tt is variable to store creditcard no. temporarily
while (tt > 0)
{
mod1 = tt % 10;
sum1 = sum1 + mod1;
tt = tt / 10;
mod2 = tt % 10;
mod2doubled = mod2 * 2;
mod2d1 = mod2doubled % 10;
mod2d2 = mod2doubled / 10;
sum2 = sum2 + mod2d1 + mod2d2;
tt = tt / 10;
sum = sum1 + sum2;
}
if (sum % 10!=0)
{
printf("INVALID3\n");//mark invalid as do not meet the criterion of sum of the digits/10 = 0
}
long firsttwodigits = creditcardno;
do
{
firsttwodigits = firsttwodigits / 10;
}
while (firsttwodigits > 100);
//check for American Express
if ((firsttwodigits / 10 == 3) && (firsttwodigits % 10 == 4 || firsttwodigits % 10 == 7))
{
printf("AMEX\n");
return 0;
}
//check for MasterCard
if (i == 16 && (firsttwodigits / 10 == 5) && (firsttwodigits % 10 == 1 || firsttwodigits % 10
== 2 || firsttwodigits % 10 == 3 ||
firsttwodigits % 10 == 4 || firsttwodigits % 10 == 5))
{
printf("MASTERCARD\n");
return 0;
}
//check for Visa
if ((i == 16 || i == 13) && (firsttwodigits / 10 == 4))
{
printf("VISA\n");
return 0;
}
else
{
printf("INVALID4\n");
}
}
The above program fails to pass CS50 check process while passing in few checks including tracking Amex/Visa/Mastercard.
It appears that I am failing to terminate the program whenever invalid digit entered.
I do understand that by using get_long for creditcardno, any possibility to enter negative number or text character is ruled out. Also, digits less than 999999999999 or greater than 9999999999999999 ruled out with this:
do { creditcardno = get_long("enter"); if (creditcardno < 999999999999 || creditcardno > 9999999999999999) printf ("INVALID\n"); return 0;
}
while (creditcardno < 999999999999 || creditcardno > 9999999999999999);
So, do I need to enter any more commands like return 0 to terminate if say 1234567890 entered? Initially, I thought that I need to keep asking the user to enter a new credit card number if say 1234567890 entered. It now appears that I need to terminate the program. Also, this somehow is contradictory to my strategy of keep asking the user to enter credit card number unless he or she enters a digit between 999999999999 and 9999999999999999.
I know there are other issues with this program but for this query to keep things manageable for me, I am seeking to address the problem of when 1234567890 entered that is currently showing:
Could not find the following in the output: <class 'pexpect.exceptions.EOF'>