Check if input is float else stop

前端 未结 6 538
[愿得一人]
[愿得一人] 2020-12-21 22:06
scanf(\"%f\", &num);

I have to check if thats a valid float variable and stop executing if it has symbols like @ or !

6条回答
  •  一生所求
    2020-12-21 22:43

    Check the return value from scanf(). It will return the number of successfully scanned inputs - in your case it should be 1. If it is not equal to one, it could be -1 which means "end of file" or 0, which means "input wasn't valid".

提交回复
热议问题