0

My code compiles and runs. Separating out the required numbers also works. The problem in my output seems to start at the processing of Luhn's Algorithim. The section of below code processes the required credit card numbers.

If it is greater than 10 separate the numbers, add the remainder to sum, and then add a 1.

If it is less than 10, it just increments the sum.

If is = to 10 then it increments the sum by 1.

Anyone have any thoughts? I have a printf line at the end of the block of code that prints out the d variable and they are wrong, that's how I know it starts here. I've tried using else if statements instead of all if, but I seem to get more errors so I just stuck with a series of if statements.

if (c1 > 10)
    {        
      d1 = 1 + (c1 % 10);
      sum = sum + d1;  
     }
if (c1 < 10)
    { 
      sum = sum + c1;
    }
if (c1 == 10)    
    { 
     sum = sum + 1;
    } 
5
  • its a small piece of the code!!!!! how can you judged one fragment by my descriptive names and the lack of comments. If you're not going to add value don't comment.
    – Mo1
    Aug 4 '15 at 7:32
  • Actually I think he can. If just some simple ifs aren't formated correctly by any standard, then it's fairly easy to extrapolate to how a much larger code snippet would look.
    – ChrisG
    Aug 4 '15 at 7:35
  • So first off, its a cut and paste and I didn't worry about proper formatting because its not relevant to my questions. You are not extrapolating, you need a baseline of information that you have not been given, you are assuming.
    – Mo1
    Aug 4 '15 at 7:39
  • Probably but it's a well based guess, that it's depending on what I see. Since by definition extrapolation is creating new information out of thin air, it's not supposed to be 100% correct. Yet I would sugest again you read the style50 guide found here manual.cs50.net/style. Don't be taken aback by our comments. We are just trying to help. I also don't understand what your code is supposed to do. Get the last digit of the card number?
    – ChrisG
    Aug 4 '15 at 7:42
  • I cleaned up the formatting and clarified the question so maybe I can get some useful information. If you don't want people to be "taken aback" let me suggest a better communication style: It is difficult to extrapolate what you are asking without more comments and variable clarity. Also, your styling is not best practice and makes the code difficult to read.
    – Mo1
    Aug 4 '15 at 8:00
1

If you are trying to make sure that, if you have 2 digits, you are summing them as individuals, this one line equation will handle that:

 c1 % 10 + c1 / 10;

Examples.

If c1 = 5 (ie, only one digit)

5%10 is 5
5/10 is 0
5+0 = 5

If c1 = 18 (ie, two digits, so 1+8)

18%10 is 8
18/10 is 1
8+1 = 9
3
  • Thanks, but do you see why the section of code I wrote doesn't work. I'm going to change mine to reflect yours, but I'd like to know where I went wrong.
    – Mo1
    Aug 4 '15 at 8:00
  • Yours looks perfectly fine, actually. I ran it with c1 = 15 and sum ended up 6 as expected. Perhaps it's the way you are getting c1? I'm not sure what c1 is meant to represent. I'm assuming it's the digit from the card?
    – curiouskiwi
    Aug 4 '15 at 8:04
  • Thanks and c1 is the digit from the card
    – Mo1
    Aug 4 '15 at 8:08

You must log in to answer this question.

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