Click here to Skip to main content
15,667,014 members
Please Sign up or sign in to vote.
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

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900