# Thread: comparing an entered value to a char

1. ## comparing an entered value to a char

How do you check if a entered value is a char and then use recursion to reenter a value.
Here is my code so far. Please help. any help would be appreciated!
Thank you!

I have been messing around with this for days and can't seem to figure it out. The char ch i just added as well as the if. I have been deleting adding. Checking. Its a nightmare.

Also having pointers too =/... Again thankk you for any help.

Code:
```/* Prompts for and inputs a fraction with input verification */
void inputFraction(int *numerator, int *denominator)
{
char ch;

printf("Please enter a fraction (a/b): ");
scanf("%d/%d", *&numerator, *&denominator);

if ((numerator || denominator) == ch)
{
printf("Plese enter a valid fraction!");
inputFraction(numerator, denominator);
}
}```

Then, checking for valid input is tricky and complex. But the general idea is to read a line using fgets or scanf (see SourceForge.net: Scanf woes - cpwiki), and then checking that the string is a digit and convert it.
To check if it's a number, you can use isdigit. To convert it, you can use strtol.

3. ## confused

ok so i read the fgets and have tried to input that into my code. i just don't know where to put it. and now i am super confused. heres what i have now and i know its wrong. if you could offer more guidiance i would appreciate it. ty

Code:
```/* Prompts for and inputs a fraction with input verification */
void inputFraction(int *numerator, int *denominator)
{
char ch;
char buffer[100];

fgets(buffer, sizeof(buffer), stdin);

buffer[strlen(buffer) - 1] = '\0';

printf("Please enter a fraction (a/b): ");
scanf("%d/%d", *&numerator, *&denominator);

if ((numerator && denominator) == ch)
{
printf("Plese enter a valid fraction!");
inputFraction(numerator, denominator);
}
}```

4. After using fgets, the easiest way to proceed would be to use sscanf to split it up. IIRC, it returns the number of "fields" correctly read. So if you extract two fields, it should return 2, or it failed. You should take a look at the documentation for sscanf.

Popular pages Recent additions