0

I have sat down and worked out my pseudo code and I'm still having difficulties In this activity. I just don't know if I am just not translating what I think into code and I'm not sure how to get the beginning numbers. Maybe I'm going at this totally wrong. I just don't want to spend hours on the wrong code.

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

int main(void)
{

    long long lastnum = 0;
    int x = 0;
    int y = 0;
    int z = 0;
    int multinum = 0;
    icount = 0;
    bnumb = 0;

do
{

    printf ("Credit Card Number ");
    long long cardn = get_long_long();
}
 while (cardn >= 0);
 {
     //get last number
 (lastnum = cardn % 10);
 (x = lastnum + x);
 (icount++);
    //get second to last number
 (cardn = cardn/10);
 (multinum = cardn % 10);
 (y = multinum * 2);
 (icount++);
    //get number for ending in zero
 (z = y + z);
 }

 If (bnumb = 34 || 37) and (icount = 15)
   {
       printf("AMEX\n", cardn);
   }
If (bnumb = 51 || 52 || 53 || 54 ||55) and (icount = 16)
{
    printf("MASTERCARD\n");
}
If (bnumb = 4) and (icount = 13 || 16)
{
    printf("VISA\n");
}
else
{
    printf("INVALID\n")
}
    return 0;
}

2 Answers 2

1

Ok, let me try to help with some mistakes I see. Maybe it won't make your code to work, but it will put you on a good track again.

Frist of all, you are asking for cedit card number wrong

do
{

    printf ("Credit Card Number ");
    long long cardn = get_long_long();
}
 while (cardn >= 0);

With this piece od code you will prompt user input again and again if card number stored in variable cardn is higher or equal to 0. And you need to do opposite. To ask him for card number again and again until user provides number higher then 0. So, this part should be

do
{

    printf ("Credit Card Number ");
    long long cardn = get_long_long();
}
 while (cardn < 0);

Next, I am not sure what you wanted but for now curly braces and parentheses don't have any role in this part, so they should be deleted

{
     //get last number
 (lastnum = cardn % 10);
 (x = lastnum + x);
 (icount++);
    //get second to last number
 (cardn = cardn/10);
 (multinum = cardn % 10);
 (y = multinum * 2);
 (icount++);
    //get number for ending in zero
 (z = y + z);
 } 

Also, in if else statement below condition with logical operators are incorrect. You can't use them like this:

(bnumb = 51 || 52 || 53 || 54 ||55)

the way they should be used is

(bnumb = 51 || bnumb = 52 || bnumb = 53 || bnumb = 54 || bnumb = 55)

Those are some mistakes I saw, but they want make your task pass the test.

0

Let's start here:

I have sat down and worked out my pseudo code

What is your pseudocode for this problem? I see a few comments in your code, but they would not suffice for a complete algorithm to solve this problem. As with most programming problems, the challenge is not the syntax but the logic thereof. Go back to the problem specification, focusing on this section:

So what’s the secret formula? Well, most cards use an algorithm invented by Hans Peter Luhn, a nice fellow from IBM. According to Luhn’s algorithm, you can determine if a credit card number is (syntactically) valid as follows:

  1. Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products' digits together.

  2. Add the sum to the sum of the digits that weren’t multiplied by 2.

  3. If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!

Try working this out by hand then type your pseudocode into your code file and keep it there to form the comments in my completed program. I suggest you edit and add to your post the precise steps you would use in your pseudocode.

To this program specifically, it is quite similar is spirit to another CS50 challenge: ISBN. Credit is essentially a more complex version of ISBN. Both involve iterating through a large number and calculating checksums. If you have yet to try ISBN, I would suggest giving it a shot first. The thinking to that problem will help you in your approach here. You want to leverage the combination of two powerful operations--modulo and integer division--repeatedly.

Another thing that would be helpful is to rename your variables. Imagine you looked back at this code in a month. Would it be easy to discern what information x, y, and z are actually meant to store? Comments and variable names, while not solving the algorithmic challenge, go a long way to making your program more readable and will help you clarify your thought process along the way.

You must log in to answer this question.

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