# Stuck with pset1 credit question. I don't know why I'm not able to get any "Valid" outputs when a valid card number is entered

``````#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <stdlib.h>

int main (void)
{

int count = 0; //Declare counter

//Get input from user

long number = get_long ("Number: ");

while (number < 0)
{

number = get_long ("Number: ");

}

//Count digits

long dcount = number;

while (dcount!=0)

{

dcount = dcount/10;

count++;

}

//Check validity

int valid = 0; //test value

if ((count != 13) && (count != 16) && (count != 15))//Filter wrong number of digits
{

printf("INVALID\n");

exit(0);
}

long dcount2 = number;

int digit; //Extracted digit

for (int i = 1; i <= count; i++)
{

digit = dcount2 % 10;

dcount2 = floor (dcount2 / 10);

if (i % 2 != 0) //If i is odd, add the value to "valid" variable
{
valid = valid + digit;
}

else //If i is even, multiply by 2
{

int d1 = digit * 2 % 10; //Ones place

int d2 = digit *2 / 10; // Tens place

valid = valid + d1 + d2;

}

if (valid % 10 != 0)
{

printf ("INVALID\n");

exit(0);
}

//Check if it fulfils each card type's specfic requirememts

else if (count == 16)// Test for mastercard and Visa
{

if ((floor (number / 100000000000000 == 51)) || (floor (number / 100000000000000) == 52) || (floor (number / 100000000000000) == 53) || (floor (number / 100000000000000)) ||(floor (number / 100000000000000) == 55))
{

printf("MASTERCARD\n");

exit(0);
}

else if (floor (number / 1000000000000000) == 4)
{

printf("VISA\n");

exit(0);
}
else
{

printf("INVALID\n");

exit(0);

}

}

else if (count == 15) //Test for Amex
{
if ((floor(number / 10000000000000) == 34) || (floor (number / 10000000000000)  == 37))

{

printf("AMEX\n");

exit(0);

}

else
{

printf("INVALID\n");

exit(0);

}
}
else if (count == 13)
{
if (floor (number / 1000000000000) == 4) //Test for 13-visa
{

printf("VISA\n");

exit(0);

}

else
{

printf("INVALID\n");

exit(0);

}

}

}

}
``````

Short answer: your code is structured wrong, and you're never actually going through the Luhn Algorithm.

General tip: work on indenting your code correctly. You can use the style50 tool (described here: https://cs50.harvard.edu/x/2020/psets/1/credit/#how-to-test-your-code) to test that things are indented and formatted correctly until it's second nature.

If you indent your code correctly, it'll be easier for you to see that this chunk:

``````if (valid % 10 != 0)
{

printf ("INVALID\n");

exit(0);
}

[code that checks the companies]
``````

is inside the `i` loop, so after a single digit is calculated, you check if that intermediate `valid` number is a valid result. Most of the time, it's not.

To fix it, make sure you're calculating the full Luhn sum, and only then check if the last digit is zero. If you have to do more problems to solve, it might be helpful to put a statement like `printf("dcount2: %li, i: %i, digit: %i, valid: %i\n", dcount2, i, digit, valid);` inside the `i` loop, so you can check that those values are correct every time your program goes through that loop. That's actually what I did when I was looking at your code, and I noticed that it only went through once, which led me to the problem.

• Thank you Llenk! I figured this out today a few hours before reading your comment. It was a good experience in learning to debug. Thank you for your help, will take the indenting advice to heart :) Jun 24, 2020 at 2:51