I would like to make a simple function that accepts a string variable and tests it to see if it's in the form of ( number/number ) so that I can use it in a fraction class, but I'm having trouble with the function. when I type h/5, it just displays it on the screen, it want prompt me to re-enter the string or anything, any ideas where I'm messing up at?
- thanks for any help-
The return type of isalpha, isspace and ispunct is int. Instead of returning false these functions return 0, and instead of true a non-zero value is returned.
When a bool is compared to an int the bool is first converted to an int, false becomes 0 and true becomes 1. Line 22 is the equivalent to writing if (isalpha(s[i]) == 1). The problem is that isalpha not guaranteed to return 1 when the character is an alphabetic character. It could return any non-zero value so the condition might never be true.
You could change the line to something like if (isalpha(s[i]) != 0) but that is not necessary. In if statements and anywhere a bool is expected an int value of 0 is considered false and anything else is considered true so you can use the return value of isalpha and the other functions directly in the if condition without using the == operator.