#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
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:
You should have:
When I tweaked that bit of code, I got the following type of terminal output.
The output agreed with an online Luhn algorithm check utility.
Give that a try.