rm1122333@yahoo.com (Rob) wrote in message news:<9d11b6b.0307261203.1fcc675@posting.google.co m>...[color=blue]
>
> My program isn't handling invalid input correctly. If the user enters
> "1abc", then it treats that as if they had just entered the number 1,
> which is incorrect.[/color]
By "incorrect," i assume that you mean "not as I intend." To guard
against something like "1abc", you might use
int option, count, length;
char buffer[100];
...
fgets (buffer, 100, stdin); /* DO NOT use gets(buffer); */
count = sscanf(buffer, "%d%n", &option, &length);
if (count == 1 && length < strlen(buffer))
/* there were extra characters after the number */
This is oversimplified, of course, because you'll have to deal with
EOF, the '\n', and possible trailing whitespace, but you get the idea.
[color=blue]
> If they enter valid input of 1, 2 or 3, then it
> works. If they enter a string such as "abc" as input, then I get an
> infinite loop where my menu is printed over and over.[/color]
See <http://www.eskimo.com/~scs/C-faq/q12.19.html>.
[color=blue]
> I'm not sure how
> to fix this. Here's my code so far. Thanks for any help.
>
>
> int option;
>
> while (1) {
> printf("choose an option [1-3]: ");
> scanf("%d", &option);[/color]
<snip>
Read <http://www.eskimo.com/~scs/C-faq/q12.20.html> for why you should
forget about scanf().
--
------------------- Richard Callwood III --------------------
~ U.S. Virgin Islands ~ USDA zone 11 ~ 18.3N, 64.9W ~
~ eastern Massachusetts ~ USDA zone 6 (1992-95) ~
---------------
http://cac.uvi.edu/staff/rc3/ ---------------