Hi, I need to write a snippet of code that takes the first character of a string( which user has inputted - call it speed ), then checks to see if the first character is a 0. If it is the program that terminates . How would I do this --- is this the best way to avoid user entering decimals, I only want integers or fractions . Cheers
Checking the first character of a string - C programming?
Assume that strVar is your string variable, just check
// on your main function
if (strVar[0] == '0') return 0;
or any other variations of that statement above.
And no, that is not the best way of avoiding users entering decimals. The best way is to read the keyboard buffer and filter the input from there, parsing into a variable the contents you really want the user to enter, and of course, at the same time parsing it to the string, if desired.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment