15,667,014 members
1.00/5 (1 vote)
See more:
The goal is to find if a credit card number is valid.
Complete Instructions Here:
Problem Set 1: C[^]
Can I please know what makes my code fail?
(I dont get running errors)

What I have tried:

C#
```#include <stdio.h>
include <cs50.h>
include <stdlib.h>
include <ctype.h>
include <string.h>
include <math.h>
int main(void) {
char name[20]; char dist[3]; long long number = GetLongLong();
sprintf(name, "%lld", number );
int length = strlen(name);
if ( (length != 13) || (length != 15) || (length != 16) ) { printf("INVALID"); return 1; }
int SUM = 0; if ( (length == 13) || (length == 15) ) { for (int a = 1; a < length-1; a+=2) { int prodig = (name[a]-48)* 2; sprintf(dist, "%d", prodig); if (strlen(dist) == 2) { SUM = SUM + (dist[0]-48) + (dist[1]-48); } else { SUM += prodig; } }
for (int a = 0; a < length; a+=2)
{
SUM += (name[a]-48);
}
}
if (length == 16) { for (int a = 1; a < 17; a+=2) { int prodig = (name[a]-48)* 2; sprintf(dist, "%d", prodig); if (strlen(dist) == 2) { SUM = SUM + (dist[0]-48) + (dist[1]-48); } else { SUM += prodig; } }
for (int a = 0; a < 15; a+=2)
{
SUM += (name[a]-48);
}
}
if (SUM%10 != 0) { printf("INVALID"); return 1; }
else { if (name[0] == 3) { printf("AMEX"); }
else if (name[0] == 5)
{
printf("MASTERCARD");
}

else
{
printf("VISA");
}
}
return 0;
}```
Posted
Updated 25-Dec-16 5:50am
v2

## Solution 1

Use the debugger. That's part of what it is there for.
Put a breakpoint at the start of the function, and step through look at closely at what is going on.

But do yourself a favour first, and format your code so it's indented consistently - it makes it a lot easier to see what it happening.
It should be reasonably obvious once you get into it what is happening, and why it's going wrong.