0
#include<stdio.h> // have four spaces before code lines
#include<cs50.h>  // this will insert them in the code block
#include<math.h>

bool checksum(long long num);
bool visa(long long num);
bool mastercard(long long num);
bool amexp(long long num);
int main()
    {
        long long card;
        do
            {
             printf("Number: \n");
             card = GetLongLong();
           }
        while (card<=0);

        if(visa(card)||mastercard(card)||amexp(card)) //checks valid format for card number
            {
                checksum(card);
            }

        if (checksum(card))  // for printing result
            {
                if(visa(card))
                    {
                        printf("VISA\n");
                    }
                else if(amexp(card))
                    {
                        printf("AMEX\n");
                    }
                if(mastercard(card))
                    {
                        printf("MASTERCARD\n");
                    }

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

    bool visa(long long num) //conditition for visa
        {   int a=0;
            while(num!=0)
                {
                    num = num/10;
                    a++;
                }
            if((a==13 || a==16)&&(num/pow(10,12)==4||num/pow(10,15)==4))  
                {
                    return true;
                }
            return false;    
        }

    bool mastercard(long long num)
        {   int a=0;
            while(num!=0)
                {
                    num = num/10;
                    a++;
                }
            if((a==16)&&((num/pow(10,14)==51)||(num/pow(10,14)==51)||(num/pow(10,14)==51)||(num/pow(10,14)==51)||(num/pow(10,14)==51)))  
                {
                    return true;
                }
            return false;    
        }

    bool amexp(long long num)
        {   int a=0;
            while(num!=0)
                {
                    num = num/10;
                    a++;
                }
            if((a==15)&&(num/pow(10,13)==34||num/pow(10,13)==37))  
                {
                    return true;
                }
            return false;    
        }    

    bool checksum(long long num) 
    {
        int a=0;
        int sum=0;
        int sum1=0;
        while(num!=0)
            {
                int b = num%10;
                if(a%2==0)
                    {
                        sum=sum+b;
                    }
                else
                    {
                        sum1=sum1+b;
                    }
                num = num/10;
                a++;
            }
        int sum2=sum+sum1;
        if (sum2%10==0)
            {
                return true;
            }
    return false;        
    }

its printing every number invalid

1
  • If your while loop goes on until num is 0, num/pow(10,14) is most likely 0, too. Also, your checksum calculation might be a bit off. In case of odd a, you'd have to add 2*b-9*(b>4) or something like that, not just b. – Blauelf Oct 26 '16 at 9:47
0

I have two observations that might help you. I am going to speak about line numbers based on the code block you provided. For example:

bool checksum(long long num); //                 Is line #05

bool visa(long long num) //conditition for visa. Is line #46

1.- On line 67 you have 51 for every part of the condition. I think you meant 51, 52, 53, 54, and 55.

if((a==16)&&((num/pow(10,14)==51)||(num/pow(10,14)==51)||(num/pow(10,14)==51)||(num/pow(10,14)==51)||(num/pow(10,14)==51)))

2.- When you check the sum you need to multiply by two the second to last digit and every other digit. The code is doing two different sums as instructed, but we need to multiply by two one of those. That would be line 102:

sum1=sum1+b;

You have to multiply b by two.

Also, when you multiply by two every other digit, they don't want the sum of the number per se. They want the sum of the digits of the number multiplied by two. For example:

If the digit is 4:

4*2 = 8 

You add 8

Easy.

If the digit is 8:

8*2 = 16
1 + 6 = 7

You add 7

A little different.

I suggest you add a line before line 102 that multiplies b by two and adds its digits. Something like this:

b = (b > 4)? 1 + (b*2)%10 : b*2;
sum1=sum1+b;

So, if b is let us say 7, 7 times 2 is 14. They want you to add 1 + 4. So you have:

1 + (b*2)%10
1 + (7*2)%10
1 +   14 %10
1 +     4

The other case is b <= 4 in which you are good with just b*2, because those results don't exceed one digit.

And this is the ternary conditional operator, but you can do it with if statements if you like:

variable = (condition)? value1 : value2;

// if condition was true  variable gets assigned value1
// if condition was false variable gets assigned value2

You must log in to answer this question.

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