Need Help Fixing My C Code for Card Number Validation

0
0
Asked By CuriousCoder123 On

Hey everyone! I'm working on a program that should read a card number and determine which company the card belongs to based on the first two digits. I'm trying to slice the card number string after converting from long to string. However, I keep getting an error saying "use of undeclared identifier 'carddigits'" even though I've already declared it in my main function. Here's the relevant code snippet for context. Any help would be appreciated!

3 Answers

Answered By CodeNinja22 On

I haven't experienced the 'undeclared identifier' error for `carddigits`, but make sure you have all the necessary headers included. Your second loop indeed has a typo; it should read:

```c
for(int v = index; v < length; v++)
```

You might also want to make sure that the indexing inside that loop references `v` instead of `u` since `u` isn't in scope there. Fixing that should help with compiling without errors.

Answered By CaffeineAddict96 On

Here’s another way to simplify your program. You could read the card number as a string directly instead of converting it back and forth between long and string:

```c
#include

int main() {
char cardNumber[20];
scanf("%19s", cardNumber);
if (cardNumber[0] == '4')
printf("VISAn");
else if (cardNumber[0] == '3' && (cardNumber[1] == '4' || cardNumber[1] == '7'))
printf("AMEXn");
else if (cardNumber[0] == '5')
printf("MASTERCARDn");
else
printf("INVALIDn");
}
```

This way, you don’t need to slice strings or convert them to integers. Would definitely make your code cleaner!

CuriousCoder123 -

Haha, I appreciate the suggestions! You've simplified it quite a bit, I'll give it a try.

Answered By TechWizard87 On

I think there's no issue with `carddigits` itself, but your `StringSlice` function has a problem in the second loop. You accidentally used a comma instead of a semicolon and referred to `u`, which isn't defined in that loop. It should look like this:

```c
for(int v = index; v < length; v++)
{
second[v - index] = s[v]; // change u to v
}
```

Also, check if your `mastercard` function is defined somewhere in your code!

CuriousCoder123 -

Thanks for pointing that out! My `mastercard` function is defined, so that should be fine.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.