-2
#include <cs50.h>
#include <stdio.h>
int main(void)
{
    long firsttwodig;
    long cardnumber;
    int sum = 0;
    int count = 0;
    cardnumber = get_long("card number:");

    firsttwodig = cardnumber;




        //first case of luhn alg
        int workingcc = cardnumber;
        while(workingcc > 0)
        {
            int lastdig = workingcc % 10;
            sum = sum + lastdig;
            workingcc = workingcc / 100;

        }


        //secound case of luhn alg
        workingcc= cardnumber / 10;
        while(workingcc > 0)
        {
            int lastdig = workingcc % 10;
            int timestwo = lastdig * 2;
            sum = sum +(timestwo % 10) + (timestwo / 10);
            workingcc = workingcc / 100;

so this is half of the code but the part where it wont work i tried everything but the luhn algo part just wont work for some reasone the code it self works its just everything is invalid thans in advanced

1 Answers1

0

Hello.

I took your code and added in some code that was missing at the end of your pasted text, and then sprinkled in a few print statements to do a quick manual debug of the process.

Basically, your only issue is that you need to also define your credit card work field to be long integers as well. Instead of:

int workingcc = cardnumber;

You should have:

long workingcc = cardnumber;

When I tweaked that bit of code, I got the following type of terminal output.

@Dev:~/C_Programs/Console/Loon/bin/Release$ ./Loon 
Enter card number: 5555555555555555
Working number: 555555555555555
Times two: 10
Times two: 10
Times two: 10
Times two: 10
Times two: 10
Times two: 10
Times two: 10
Times two: 10
The sum is: 48 which is invalid
@Dev:~/C_Programs/Console/Loon/bin/Release$ ./Loon 
Enter card number: 5555555555555557
Working number: 555555555555555
Times two: 10
Times two: 10
Times two: 10
Times two: 10
Times two: 10
Times two: 10
Times two: 10
Times two: 10
The sum is: 50 which is valid
@Dev:~/C_Programs/Console/Loon/bin/Release$ 

The output agreed with an online Luhn algorithm check utility.

Give that a try.

NoDakker
  • 3,390
  • 1
  • 10
  • 11