PaulS:
Objection. Already asked and answered, your honor.
It hasn't been answered. I hate to do this as it will likely mess with the thread, but for your benefit, let's change the parameters for a moment. How about serial control of the speed of a motor:
Two possible inputs.
- Text (start - stop)
- Integer (speed 0-100)
You don't know what the next serial input is. How do determine atoi() should return 0 for motor speed or returned 0 for invalid data?
In a perfect world, start and stop can be filtered first with a simple strstr() then everything else to be passed to atoi(). Unfortunately we are not in a perfect world, and everything else includes start9, 0', st0p, 1000 which are all invalid entries but which atoi() will return 0 and set the speed to 0 unless trapped. How do you form the trap?