0
#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'>

enter image description here

1 Answer 1

2

I took a quick glance at the code. A couple suggestions that may resolve your issues.

First, the standard is that a program returns 0 when it completes successfully, and returns non-zero when there's an error, like invalid card numbers. So, shouldn't many of those return 0 statements actually be return 1;? It's been a long time, but wasn't there something about that in the instructions?

Next, there are some missing at INVALID2, 3 and 4:

else
{
    printf("INVALID4\n");
}

Shouldn't there be a return 1 statement here too? Anytime the program encounters an invalid number, it should print and then exit. If it doesn't exit, it will continue to process and could print a false positive message. Also, check50 isn't seeing certain internal messages that get generated, thus the cryptic message that you see.

Refresh my memory. Do the program instructions say to ask for a new number under any conditions, or to just print INVALID and terminate? Further, tacking numbers on the end of the word INVALID will also cause check50 to fail. Obvious that you're using that to see where it fails (good idea), but once you get it running correctly, those messages need the numbers removed.

If this doesn't cure it, post a message and I'll do a deep dive on the code. ;-)

2

You must log in to answer this question.

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