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
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.
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!
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!
Thanks for pointing that out! My `mastercard` function is defined, so that should be fine.
Haha, I appreciate the suggestions! You've simplified it quite a bit, I'll give it a try.