+ 4

C - verify fgets in some possibilities

const char *test[4] = {"one", "two", "three"}; fgets(my_str, 10, stdin); how can I check if my_str == "one" or "two" or "three" but not like that : ? if my_str=="one" || my_str=="two"...

4th Feb 2019, 7:45 PM
NoxFly
NoxFly - avatar
3 Respuestas
+ 6
Hi NoxFly ドリアン here try strcmp (http://www.cplusplus.com/reference/cstring/strcmp/) but remember fgets doesn't remove the "\n" from the buffer, so you have to clean it up manually, if you don't it will make comparison always wrong because of the "\n" 😁
4th Feb 2019, 8:20 PM
Ipang
+ 4
thanks I'll try :D
4th Feb 2019, 8:21 PM
NoxFly
NoxFly - avatar
+ 2
you can do this loop, length of array is small. Either you can use sorting.
5th Feb 2019, 3:08 AM
lk👽