0

I'm working on the credit problem in pset1, and this is only part of what should be done. All I'm trying to do here is to multiply every other number (from the cc number entered by the user) by two, starting with the second-to-last digit, and add the digits of the products of those numbers, and save the result in a variable called "sum". For some reason, every time i run the code, i get a different "sum" number. Does anyone know why this is happening? Thank you! Here's the code:

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

int main (void)
{
long long cc_number;

do
{
    cc_number = get_long_long ("Please enter a credit card number: ");

}
while (cc_number <= 0);

//the following long_long variable is for the sum of the digits that start with the last digit, skip one, then third digit, and
//so on, so it is the sum of the digits that are in the "odd number positions". eg: positions 1,3,5,..etc., with the last digit
//position being position 1.

//long long previous_sum = 0;

int count = 0;
//long long digit;
while (cc_number > 0)
{
    cc_number /= 10;
    count ++;
    cc_number--;
}
//int indv_numbers [count];

int indv_digit [16];

while (cc_number > 0)
{
    indv_digit [count] = (cc_number % 10);
    cc_number /= 10;
    count ++;
}
//the following int variable: int indv_digit_addition, was made to enable adding the digits of the products of the numbers starting from the second-to-last
// digit, skipping one, then adding the next, then skipping one, etc.
int sum = 0;

for (count = 1; count <=14; count+=2)
{
    int indv_digit_times_two = (indv_digit [count] * 2);
    if (indv_digit_times_two != 0)
    {
    int remainder = indv_digit_times_two % 10;
    sum = sum + remainder;
    indv_digit_times_two = indv_digit_times_two / 10;
    }
    else
    {
        int remainder = indv_digit_times_two;
        sum = sum + remainder;
    }
}
printf ("%d", sum);






}

2 Answers 2

1

After

while (cc_number > 0)
{
    cc_number /= 10;
    count ++;
    cc_number--;
}

cc_number is -1, so second while loop is never entered. Which leaves the indv_digit uninitialised completely.

Also, "10" has 1 digit, "100" has 2? Where does this rogue cc_number--; come from?

As a side note: Nobody is going to pass those, but some long long int values are 19 digits. Your loop would write over the array boundaries. If you didn't store all the digits separately, this wouldn't pose any problem.

Back in the code.

while (cc_number > 0)
{
    indv_digit [count] = (cc_number % 10);
    cc_number /= 10;
    count ++;
}

(the loop never entered because of the one before) also counts digits, and does so correctly if count is initially 0. Maybe remove the first while loop then?

for (count = 1; count <=14; count+=2)

This gets somewhat ridiculous. You made such an effort to get the number of digits into count, and now you throw this number away.

BTW, this loop runs exactly 7 times, with values 1, 3, 5, ..., 13. There are credit card numbers with more than 15 digits.

0
0

There are a few problems with your code, here are some of them (not necessarily exhaustive so I may have missed something):

  1. Unsure what you are trying to do in this part of your code

    while (cc_number > 0) { cc_number /= 10; count ++; cc_number--; }

  2. Using the same general framework, get the remaining digits in the "even positions" as you call them. Get every other number starting from 2nd to last digit, multiply it by 2, then check if it's >10. If so, add the 2 digits together, then add everything to the running total from step 2.

  3. This line:

for (count = 1; count <=14; count+=2) limits you to card numbers of length 14, but card numbers can have up to 16 digits.

Hope this helps.

You must log in to answer this question.

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