Connecting Tech Pros Worldwide Forums | Help | Site Map

help with infinite loops and scanf

Rob
Guest
 
Posts: n/a
#1: Nov 13 '05
I'm stuck in a program I'm making. I'm supposed to have a menu that
asks the user to choose an option between 1 and 3. If the user chooses
a valid option, i.e. types a number from 1 to 3, then appropriate
things happen for the option they chose. After each option is
executed, the menu should come back asking them to choose an option
again. It should loop and do this forever. My problem is, I have to
detect the user entering anything other than the integers 1, 2 and 3,
and it's not working.

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. 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. 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);

if (option != 1 || option != 2 || option != 3) {
printf("\nInvalid option.\n");
} else if (option == 1) {
/* do something */
} else if (option == 2) {
/* do something */
} else if (option == 3) {
/* do something */
}
}

ak
Guest
 
Posts: n/a
#2: Nov 13 '05

re: help with infinite loops and scanf


On 26 Jul 2003 13:03:17 -0700, rm1122333@yahoo.com (Rob) wrote:

|I'm stuck in a program I'm making. I'm supposed to have a menu that
|asks the user to choose an option between 1 and 3. If the user chooses
|a valid option, i.e. types a number from 1 to 3, then appropriate
|things happen for the option they chose. After each option is
|executed, the menu should come back asking them to choose an option
|again. It should loop and do this forever. My problem is, I have to
|detect the user entering anything other than the integers 1, 2 and 3,
|and it's not working.
|
|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. 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. I'm not sure how
|to fix this. Here's my code so far. Thanks for any help.
|

try using some other input function than scanf();
for instance fgets( ), since you then have more
control on the input.

after reading the string with fgets you can parse
it by using for instance strtok()

use a switch statement instead of several if -else's
a bit more readable when there are many if-else's


hth/ak


--
g a n d a l f @ p c . n u
Burne C
Guest
 
Posts: n/a
#3: Nov 13 '05

re: help with infinite loops and scanf



"Rob" <rm1122333@yahoo.com> wrote in message news:9d11b6b.0307261203.1fcc675@posting.google.com ...[color=blue]
> I'm stuck in a program I'm making. I'm supposed to have a menu that
> asks the user to choose an option between 1 and 3. If the user chooses
> a valid option, i.e. types a number from 1 to 3, then appropriate
> things happen for the option they chose. After each option is
> executed, the menu should come back asking them to choose an option
> again. It should loop and do this forever. My problem is, I have to
> detect the user entering anything other than the integers 1, 2 and 3,
> and it's not working.
>
> 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. 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. 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]: ");[/color]

You can flush the stdin each time to prevent the looping problem.

Add the line here:
fflush(stdin);
[color=blue]
> scanf("%d", &option);
>
> if (option != 1 || option != 2 || option != 3) {
> printf("\nInvalid option.\n");
> } else if (option == 1) {
> /* do something */
> } else if (option == 2) {
> /* do something */
> } else if (option == 3) {
> /* do something */
> }[/color]

First, you shouldn't use OR in the first "if" line, I think there is a logical problem. You can use
AND

if (option != 1 && option != 2 && option != 3)

or even better:

if (option == 1) {
/* do something */
} else if (option == 2) {
/* do something */
} else if (option == 3) {
/* do something */
}else
printf("\nInvalid option.\n");

or using switch.
[color=blue]
> }[/color]

--
BC


amanayin
Guest
 
Posts: n/a
#4: Nov 13 '05

re: help with infinite loops and scanf


Rob wrote:


My problem is, I have to detect the user entering anything other than the
integers 1, 2 and 3,


if (option != 1 || option != 2 || option != 3) {
printf("\nInvalid option.\n");
}

change line above to

if (option < 0 || option >3) {
printf("\nInvalid option.\n");
}
Emmanuel Delahaye
Guest
 
Posts: n/a
#5: Nov 13 '05

re: help with infinite loops and scanf


In 'comp.lang.c', "Burne C" <nobody@notexist.com> wrote:
[color=blue]
> You can flush the stdin each time to prevent the looping problem.
>
> Add the line here:
> fflush(stdin);
>[/color]

No. fflush () is only defined for output streams.

--
-ed- emdelYOURBRA@noos.fr [remove YOURBRA before answering me]
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
<blank line>
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
ak
Guest
 
Posts: n/a
#6: Nov 13 '05

re: help with infinite loops and scanf


On Sun, 27 Jul 2003 09:15:44 +0000 (UTC), amanayin <nglen702@netscape.net>
wrote:

|Rob wrote:
|
|
| My problem is, I have to detect the user entering anything other than the
|integers 1, 2 and 3,
|
|
|if (option != 1 || option != 2 || option != 3) {
| printf("\nInvalid option.\n");
| }
|
|change line above to
|
|if (option < 0 || option >3) {
| printf("\nInvalid option.\n");
| }

yes well if you read the input into a string buffer pszBuf
using fgets

then you can extract the contents by using sscanf or
any other parsing function:

sscanf( pszBuf, "%d", &option );

you have to add some error handling around it though like
check return value of sscanf to see if an integer was read.

hth
/ak



--
g a n d a l f @ p c . n u
Cousin Ricky
Guest
 
Posts: n/a
#7: Nov 13 '05

re: help with infinite loops and scanf


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/ ---------------
Neil Cerutti
Guest
 
Posts: n/a
#8: Nov 13 '05

re: help with infinite loops and scanf


In article <9d11b6b.0307261203.1fcc675@posting.google.com>, Rob
wrote:[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. 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. I'm not sure how to fix this. Here's my code so
> far. Thanks for any help.[/color]

You need to:
* Ensure that your prompt gets printed/flushed before scanf is
called.
* Pay attention to the return value of scanf.
* Ignore trailing characters up to the next newline.
* Handle error conditions and EOF.

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int option;
int n;
while (1) {
printf("Choose an option [1-3]: ");
fflush(stdout);
n = scanf("%d%*[^\n]", &option);
if (n == 1) {
if (option < 0 || option > 3) {
printf("Invalid option. Please try again.\n");
} else {
puts("OK\n");
break;
}
} else if (n == EOF) {
printf("Unexpected end of input. Terminating.\n");
return EXIT_FAILURE;
} else {
printf("Invalid option. Try again.\n");
clearerr(stdin);
scanf("%*[^\n]");
}
}
/* Do something with option */
return 0;
}


--
Neil Cerutti
Closed Thread


Similar C / C++ bytes