Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

scanf("%f", &num);

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

Suggestions?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
973 views
Welcome To Ask or Share your Answers For Others

1 Answer

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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...