0

I am very new at coding and taking CS50. I am currently working on the credit problem in week 1. I finished coding but when I ran check 50 my code only seemed to work for some of the card numbers and I can't figure out why the other ones don't run.

This is my code

#include <stdio.h> #include <cs50.h> #include <math.h> int main(void) { // get card number long int n; do { n = get_long_long("Credit Card Number:"); } while (n < 0); // calculate checksum int x = n; int sum1 = 0 int sum2 = 0 int total = 0 int last1; int last2; int d1; int d2; int dbl; do { last1 = x % 10; x = x / 10; sum1 = sum1 + last1; last2 = x % 10; x = x / 10; dbl; = last2 * 2; d1 = dbl % 10; d2 = dbl / 10; sum2 = sum2 + d1 + d2; } while (x > 0); total = sum1 + sum2; if ((total % 10) != 0) { printf("INVALID\n"); return 0; } // check length of card number long int y = n; int count = 0; do { y = y / 10; count++; } while (y > 0); if ((count ! = 13) && (count ! = 15) && (count ! = 16)) { printf("INVALID\n"); return 0; } // check starting digits long int z = n; do { z = z / 10; } while (z > 100) // visa if (z / 10 == 4) { printf("VISA\n"); } // mastercard else if ((z / 10 == 5) && (z % 10 > 0) && (z % 10 < 6)) { printf("MASTERCARD\n"); } // AMEX else if ((z / 10 == 3) && ((z % 10 == 4) || (z % 10 == 7))) { printf("AMEX\n"); } else { printf("INVALID"); } } 

and these are the error messages I receive when I run check 50

:) credit.c exists :) credit.c compiles :) identifies 378282246310005 as AMEX :( identifies 371449635398431 as AMEX expected "AMEX\n", not "INVALID\n" :( identifies 5555555555554444 as MASTERCARD expected "MASTERCARD\n", not "INVALID\n" :) identifies 5105105105105100 as MASTERCARD :( identifies 4111111111111111 as VISA expected "VISA\n", not "INVALID\n" :( identifies 4012888888881881 as VISA expected "VISA\n", not "INVALID\n" :( identifies 4222222222222 as VISA expected "VISA\n", not "INVALID\n" :) identifies 1234567890 as INVALID :) identifies 369421438430814 as INVALID :) identifies 4062901840 as INVALID :) identifies 5673598276138003 as INVALID :) identifies 4111111111111113 as INVALID :) identifies 4222222222223 as INVALID 

the frowny faces are the ones that come out incorrect so let me know if anyone knows why they're not coding correctly!

1
  • Have you tried decomposing your if chain to understand where the INVALID stops for each failcheck? I have the same problem, but I only get 3 failchecks (422(...) is one of them) and I think there's actually a bug with the check50 script. I posted a question about it a couple minutes ago. Commented Aug 1, 2024 at 17:53

1 Answer 1

0

One issue I see straight away (there may be others?) is that you are correctly taking in the user's input as a long

long int n; 

but after your loop, you are then storing that value in an int

int x = n; 

Doing so means that you will lose half the bits of the value.

Try changing x to a long int to see if that solves your problem.

1
  • Hi, I just tried it and the same 5 card numbers are still not gong through correctly. looks like there are some other issues aswell Commented Jul 16, 2021 at 14:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.