0

I've submitted my credit problem after testing with possible values and everything seems to work just fine as planned, except that I got some errors that certain numbers could not be tested as INVALID in check50. CODE HERE

Screenshot showing error with check50

The surprising thing is that when I tried testing these numbers on the compiler, it will generate the required INVALID.

enter image description here

So what could I be missing? I can't figure this out as everything looks fine.

EDITED TODAY:
So I figured out that the above happened because all the tested numbers had something in common i.e last digit of checksum evaluates to zero even though the number wasn't really a card number with a valid brand.

The fix I guess is to check not just for checksum but also for valid brand.

 if (((checksum % 10) == 0) && (brand == "VISA" || brand == "MASTERCARD" || brand == "AMEX"));

But, this generated a new error:

    card.c:90:42: error: result of comparison against a string literal is unspecified (use strncmp instead)
      [-Werror,-Wstring-compare]
    if (((checksum % 10) == 0) && (brand == "VISA" || brand == "MASTERCARD" || brand == "AMEX"))
                                         ^  ~~~~~~
card.c:90:61: error: result of comparison against a string literal is unspecified (use strncmp instead)
      [-Werror,-Wstring-compare]
    if (((checksum % 10) == 0) && (brand == "VISA" || brand == "MASTERCARD" || brand == "AMEX"))
                                                            ^  ~~~~~~~~~~~~
card.c:90:86: error: result of comparison against a string literal is unspecified (use strncmp instead)
      [-Werror,-Wstring-compare]
    if (((checksum % 10) == 0) && (brand == "VISA" || brand == "MASTERCARD" || brand == "AMEX"))
                                                                                     ^  ~~~~~~
3 errors generated.
<builtin>: recipe for target 'card' failed
make: *** [card] Error 1
$ help50 make card
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wshadow    card.c  -lcrypt -lcs50 -lm -o card
card.c:90:42: error: result of comparison against a string literal is
      unspecified (use strncmp instead) [-Werror,-Wstring-compare]
    if (((checksum % 10) == 0) && (brand == "VISA" || brand == "MASTERCA...
                                         ^  ~~~~~~
card.c:90:61: error: result of comparison against a string literal is
      unspecified (use strncmp instead) [-Werror,-Wstring-compare]
  ...% 10) == 0) && (brand == "VISA" || brand == "MASTERCARD" || brand == "AM...
                                              ^  ~~~~~~~~~~~~
card.c:90:86: error: result of comparison against a string literal is
      unspecified (use strncmp instead) [-Werror,-Wstring-compare]
  ...== 0) && (brand == "VISA" || brand == "MASTERCARD" || brand == "AMEX"))
                                                                 ^  ~~~~~~
3 errors generated.
<builtin>: recipe for target 'card' failed
make: *** [card] Error 1

Asking for help...

card.c:90:42: error: result of comparison against a string literal is

Not quite sure how to help, but focus your attention on line 90 of card.c!
$ 
0

You can't compare strings with ==.

See here for more information.


The variable brand is just a pointer to a string (it points to a place stored in memory) while "VISA" is a string literal, meaning it is the actual data instead of a pointer to the data. Therefore, you need to use the strcmp function instead to get accurate results.

For example, you could say: strcmp(brand, "VISA") == 0 (which checks to see if the two strings are the same).

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .