0

Why won't this code print every other digit in a long long number (in this case, it's suppposed to be a credit card number), and add these numbers up? You'll notice I commented out several lines of code, but some lines are actual comments to explain what I mean by the long long odd_group_sum variable. My plan was to calculate the sum of the "odd-numbered positioned" digits in the credit card number first (ie: the number in the first space, then third space, fifth space, etc.) , then after figuring that out, i was going to attempt finding a way to multiply the "even-number positioned digits" by two, and add their digits, and proceed with the code basically. The point of all of this is to code a verifier of authenticity of credit card numbers.

Please excuse my unpleasant-looking code, I'm a beginner just learning :)

(in the commented out lines, you'll see I have variables for specific positions of the digits, and I was going to try to do the code the long way without abstracting the process, thinking there was no short way, but then I thought maybe I can do this in a shorter way, and I got stuck.)

Thank you!

#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 );

 //long long first_digit;
 //long long third_digit;
 //long long fifth_digit;
 //long long seventh_digit;
 //long long ninth_digit;
 //long long eleventh_digit;
 //long long thiteenth_digit;
 //long long fifteenth_digit;
 // 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: position 1, 3, 5, etc., with last digit being
 //position 1.
 //long long odd_group_sum = 0;
 long long digit = 0;
long long previous_sum = 0;

   while (cc_number >1); 
   {
        printf("%lld\n", cc_number%10);
        scanf ("%lld", &digit);
        printf("Sum so far of %lld plus %lld is %lld", previous_sum, 
digit, previous_sum + digit);
        scanf("%lld", &previous_sum);
        cc_number / 100;

   }


}

1 Answer 1

0

First: ; is the empty statement. So while (cc_number > 1); will repeat doing nothing. Remove the semicolon.

Also, I'd use > 0 instead of > 1. Just in case first digit is 1.

And

cc_number / 100;

calculates cc_number / 100 and ignores the result. You likely meant

cc_number /= 100;

which is about same as

cc_number = cc_number / 100;
2
  • Thank you for your help, but this hasn't solved the problem. If anyone has any other ideas on how to solve this I would appreciate it!
    – John
    May 24, 2018 at 20:56
  • I hoped you would figure out after fixing the infinite loop. Inside the loop, I would not expect scanf (which requests additional input from the user) but something like digit = cc_number % 10;
    – Blauelf
    May 25, 2018 at 6:00

You must log in to answer this question.

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