0

I'm starting the week 1 credit problem, however, when trying to set together every second digit it simply does not print. Could you help me identify where I went wrong? Thanks!

#include <stdio.h>
#include <cs50.h>
int main(void)
{
     long credit_number = get_long("Enter a credit card number: "); //asks for credit card number

int i = 0;
int j = 0;
int add_digits = 0;
int multiplied_digits = 0; // these four lines set the variables

while(credit_number >= 0) // runs the loop until the credit card number is l
{
    i = credit_number % 10; credit_number = credit_number - i; // sets i as the reaminder of credit card number / 10, oit then subtracts i from credit card number so the nest time it moves to the next digit
    add_digits = add_digits + i; // creates a variable with all other digits starting from the first one at the end
    
    j = credit_number % 10; credit_number = credit_number - j; // same as before but applied to the last digit
    multiplied_digits = multiplied_digits + j; //craetes variable with all second digits starting from the second one at the end
}
printf("%i\n", multiplied_digits);   // to test if it creates a list of the digits; hint: it doesn't

}

Screenshot of code

1

The program is stuck in an infinite loop. Look at the while loop setup:

while(credit_number >= 0) // runs the loop until the credit card number is l
{
    i = credit_number % 10; credit_number = credit_number - i;

The first pass subtracts the least significant digit from the cc number, so 371449635398431 becomes 371449635398430. From there on, i is always 0, so nothing ever changes. You need to rethink how to implement this. You might try watching the video shorts for this pset.

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

You must log in to answer this question.

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