0

Update: I already figured out what's wrong, it's the function of adding the digits together. Some also pointed out how some parts of this code is unnecessary so I deleted it and it definitely helped! I was able to point out the problem more easily. Thank you guyss, appreciate it I'm trying to figure out what I did wrong in the first step of the credit problem in CS50. Im trying to create a program that implements the Luhn algorithm. I already figured out how to add every other digit starting from the least significant one. But I have a problem in the part of multiplying every other digit by 2, and adding the digits of the products together individually. I first decided to do an array but I tried to do now a while loop since someone suggested it can be solved thru a loop. When I type in the credit number 4003600000000014, it returns the value of 24 instead of 13. I cant figure out the logical error, and ive already tried to solve it for hours. Would definitely appreciate your help!

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

int sum_digits(int number2);
int get_number_digits(long x);
int times_two(int x);

int main(void) {
  
  //Prompt for input
  long number = get_long("Card Number: \n");
  //initialize number to two different variables for [1. multiplying by
  //2, adding the products]
  //[2. adding every other digit]
  long number2 = number;
  long number3 = number;
  
  //Calculate checksum
  
  //Multiply digits by 2 alternately starting from the tens place, then
  //add the digit of the products NOTE: edit loop, something is wrong
  long remainderr2, currentnumber2, product, initialsum;
  currentnumber2 = number3 / 10;
  number3 = currentnumber2;
  
  int finalsum = 0;
  while (currentnumber2 > 0) 
  {
    
    number3 = currentnumber2;
    remainderr2 = currentnumber2 % 10;
    product = times_two(remainderr2);
    initialsum = sum_digits(product);
    finalsum = finalsum + initialsum;
    currentnumber2 = currentnumber2 / 10;
    currentnumber2 = currentnumber2 / 10;
  }
  printf("%ld", finalfinalsum);
}

//Function that adds the digits of a number
int sum_digits(int number2) {
  int secondstep, remainderr, currentnumber, sum;
  currentnumber = number2;
  secondstep = 0;
  while (number2 > 0) {
    number2 = currentnumber;
    sum = 0 + secondstep;
    remainderr = currentnumber % 10;
    secondstep = sum + remainderr;
    currentnumber = number2 / 10;
    
  }
  return sum;
  
}

//Function that multiplies int by 2
int times_two(int x) {
  x = x * 2;
  return x;
}

       
chris10
  • 33
  • 6
  • you didn't specify the platform, maybe the long is too short in your platform to store a 15digit number. You should try using long long. – Pablo Yaggi Aug 02 '20 at 15:23
  • 2
    You should never use integer for credit-card number *or* phone numbers. The simply can't handle all the requirements needed (like possible leading zeros). – Some programmer dude Aug 02 '20 at 15:31
  • 1
    pls, provide your required input/output, and also can u pls give me your code without dependency, i don't have cs50.h, so can't run your code? – reyad Aug 02 '20 at 15:37
  • i changed it to long long and it still doesn't return 13 :( – chris10 Aug 02 '20 at 15:38
  • `finalfinalsum = 0 + finalsum;` what is this line? and why did u write it like this? you could just write `finalfinalsum = 0 + finalsum;` – reyad Aug 02 '20 at 15:39
  • pls, tell us exactly where is the first mismatch(or, error) occuring and what is that error, for which input, and what is the code's output and what is the correct output. Provide us that code segment, it'll be easier for us to answer the question – reyad Aug 02 '20 at 15:42
  • i guess the required inputs are 13-16 digit credit card number. In this part the program should multiply the digits to 2 starting from the second least significant digit. Then add the digits of those products together, so that should be the output. I dont understand the part with code without dependency though? – chris10 Aug 02 '20 at 15:42
  • `I dont understand the part with code without dependency though`, no problem, just provide answer my previous comment...and if u pls do it fast... – reyad Aug 02 '20 at 15:43
  • okay i will delete the finalfinalsum = 0 + finalsum; line, i guess it's unnecessaryy – chris10 Aug 02 '20 at 15:44
  • the output of the code is 32, instead of 13, that's the error I was referring to but so far there are no syntax error – chris10 Aug 02 '20 at 15:45
  • and what was the input? was it 4003600000000014? and also update ur latest code(the modified one). – reyad Aug 02 '20 at 15:49
  • yes, that was the inputt – chris10 Aug 02 '20 at 15:51
  • I guess you've some problem in ur algorithm implementation. I have already change all `int` and `long` to `long long` type and also, print using `printf("%lld", finalfinalsum);`, so, pls check again ur algorithm again and even if u can't solve ur problem, then question again or update this question – reyad Aug 02 '20 at 15:54
  • okay, thank you! ive been racking my brain for what i did wrong with the algorithm but i cant figure it outtt – chris10 Aug 02 '20 at 15:57
  • 1
    As mentioned earlier, the appproach is wrong. Instead of thinking "a number is an integer" it is better to work with a digit string. Another reason besides not knowing how many leading zeros an integer has, is that the information *arrived* as the sequence of digits you require. They were converted to an integer and now you have to pick it apart to what was entered in the first place. – Weather Vane Aug 02 '20 at 16:22
  • By the way, in case any of you are wondering, I believe chris10 is using CS50 IDE as the platform. – M-Chen-3 Aug 03 '20 at 02:28

0 Answers0