Hey I got a question about comparing strings. I have a code similar to below:
Code:
typedef char * string;
void main()
{char choice[10];
printf("Enter number:\n");
scanf("%s", choice);
int result = getnum(choice);
printf("Number: %d", result);
}
int getnum(char choice[10])
{string numbers[] = {"zero","one", "two", "three", "four"};
int i;
for(i=0;i<5;i++)
{
if(strcmp(numbers[i], choice) == 0)
{
int result = i;
return result;
}
}
}
The purpose of the code above is basically to turn a string into a number. Here's the problem I'm having if the user enters a valid number (One of the numbers in the array string) then the result is correct however if the result is not in the array it still returns a "1" for some reason.
Any reason for this? I'm new to C programming so excuse my amateur script