Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 3923

General • C program, flush input, scanf(), menus, etc.

$
0
0
Hi,

In a C program for the Pico I'm creating text menus for sensor control via a UART serial monitor.

A well known gotcha with scanf() on any platform is that if you request an integer, like scanf(%i, &num); and the user enters a non-numeric character, the program will go into an infinite loop. I've put scanf into a while loop that checks for an invalid character by checking if the return value is != 1 but I then am forced to run a while(1); and basically shut down the program for a bad character. Methods of clearing the input buffer by consuming characters until a newline or EOF is reached do not seem to work on the Pico.

Is there a better way, getchar() maybe? Also is there a way to warm reboot the Pico with a command?

What's your advice for text menus as far as C statements, I'm using switch-case statements.

thanks!

Statistics: Posted by breaker — Tue Apr 02, 2024 10:15 pm



Viewing all articles
Browse latest Browse all 3923

Trending Articles