0

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

}
2
  • 1
    Can you please explain what the problem is? What are the symptoms? What is the code doing or not doing? Please edit the question and add this information. It is important that you are able to identify and describe a program's incorrect behavior. – Cliff B Apr 8 '19 at 22:10
  • I have a problem in detecting the first && second digit conditions.. the above code doesn't sound to detect it properly and I don't know how to fix it.. – Ahmed-ElKodsh Apr 9 '19 at 10:08
0

There seems to be an important component of credit card number validation missing. From the spec:

American Express uses 15-digit numbers, MasterCard uses 16-digit numbers, and Visa uses 13- and 16-digit numbers.

This program will return VISA for card number 4062901840. It starts with a 4, it has a valid checksum, but it is only 10 digits in length.

You must log in to answer this question.

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