2

Hi i am a beginner and i have been trying to solve this set but with no avail.

I tried to use what was taught in the first lesson and did search for some help online when i encounter errors. However, i am still unable to execute the code properly. What may have gone wrong?

The question apparently require me to check if the credit card number is valid. First I want to make sure the credit card number consist of 16digits, then I tried to identify the respective digits. I then multiply the alternate numbers starting from second last digit by 2. After which I tried to identify any 2digits value (e.g 18) from the multiplied result and sum it (eg 1+8). Then add the sum of the another set of alternate digits with the sum of the multiplied amount. And if the last digit of the sum=0, then it should return approve.

I managed to get no error for my codes but it keeps giving me “disapprove” even if the credit card number should return “approve” please help thank you.

this is my code:

            #include <stdio.h>
            #include <cs50.h>
            #include <math.h>


            int count=0;//no. of digits


            int sum1=0;
            int sum2;
            int sum3;
            int main(void)
            {
            long long cc = 0;
            do
            {
            printf ("please input your credit card no. \n cc.:"); // prompt
            cc=get_long_long();}
            while (cc< 0);

            //determine if it has valid number of digits
             long long digits = cc;
              while (digits>0)
             {
             digits/=10;
             count++;
        }

            if (count!=16)
            {printf("invalid\n");

          }
        else{


    for(int i=1;i<=count; i++)
    {       double a=0;
            double b=0;
            a+= pow(10,i);
            b+= pow(10,(i-1));
            long long c;
            long long d;
            c=a;//convert double to int so % can be used
            d=b;

        if(i%2==0)//identify even position digits
         {

           int even_digits;
            int xsum=0;
            even_digits=2*(((cc%c)-(cc%d))/d); //2* even position digits

            if(even_digits>9)//identify 2 digits values after 2* e.g. 18
            {
                xsum+=(even_digits/10)+(even_digits%10);//sum the 2 value digits e.g. 1+8=9
            }
            else
            { sum1+=even_digits; } //sum of all the even position digits that has only 1 value
            sum2=sum1+ xsum;//sum of all the even position digits

        }
        else
        {

            int odd_digits=0;
            odd_digits=((cc%c)-(cc%d))/d;//identify all value of odd position digits
            sum3= sum3+odd_digits;//sum all values on odd position
        }}

            int sum4=0;
             sum4 = sum3+sum2; //sum of even and odd position digits

    if(sum4%10==0)
    {
        printf("approve\n");
    }
    else
    {
        printf("disapprove\n");
    }

    }

}

  • Can you edit the question and describe exactly what kind of problems you are having? We need more detailed information, along with some description of what you've done to try and resolve the problem. As this question stands, it is too vague. – Cliff B Jul 6 '18 at 11:25
  • Thank you for the reply. I have edited my question. Sorry for being unclear with my question. Hope it is clearer now – Trevor Lim Jul 7 '18 at 12:24
  • I don't quite understand your code but maybe try a different approach.. Possibly performing luhn's algorithm first and then validating card length at the end. Not all card lengths are 16 digits – PaleFire Jul 9 '18 at 8:46
  • Also you shouldn't need to have a variable after the '%' symbol. i = CC % 10 would give you the last digit, which you can then subtract from CC and divide CC by 10 for next digit - sorry if this is a bit vague – PaleFire Jul 9 '18 at 8:50
0

There are a number of issues.

First, the requirement says ... return INVALID, not invalid or disapprove. "approve" is also not going to work. If a card is good, it should return the name of the card - VISA, AMEX or MASTERCARD.

Card number lengths are 13, 15 and 16, not just 16. And, you need to know which type of card goes with which length.

The instructions are very specific. This is one of the hidden lessons of the early psets. If they say something specific, do exactly what it says, not something close. This is particularly important in a team programming environment where many people are working on parts of a program that have to fit together correctly later. Close doesn't cut it.

Most importantly, the accumulator math for the even digits in the code contains two problems. sum1 doesn't reset between passes, so its value from the last time even_digits was <= 9, it retains its value and is added to the calculation when even_digits > 9. Next, sum2 is not adding back its own current value to accumulate the correct total for even digits.

Also, there are 3 vars, sum1, sum2, and sum3, that are declared outside of main. This is called declaring a global variable. Vars should never be declared as globals unless there's a really, really good reason. That's in a later lesson.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • Thank you, i managed to identify the issue after going through the next lesson and by using debug50. I picked up the question from the walkthrough video, so I might have missed some specific instructions. I have no idea where i can find the hidden lessons/ instructions. – Trevor Lim Jul 18 '18 at 4:05
  • You can't find those "hidden lessons", which is what I call them. They're lessons that you'll pick up along the way. To reiterate, this one is that code should be written exactly to the spec, not close. When multiple people are working on various parts of a program, they have to be written to specific requirements so that they will all fit together later. ;-) – Cliff B Jul 18 '18 at 5:27
  • Haha thank you and appreciated the help. Previously i do not have the specifications of the problem set, hence i wasnt aware of the different card number lengths to consider when doing up the coding. Managed to figure it out, but now stuck at crack.c. LMAO. might skip it and come back to that after more lessons. – Trevor Lim Jul 18 '18 at 10:20

You must log in to answer this question.

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