Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > help with infinite loops and scanf

Reply
Thread Tools

help with infinite loops and scanf

 
 
Rob
Guest
Posts: n/a
 
      07-26-2003
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 */
}
}
 
Reply With Quote
 
 
 
 
ak
Guest
Posts: n/a
 
      07-26-2003
On 26 Jul 2003 13:03:17 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed) (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
 
Reply With Quote
 
 
 
 
Burne C
Guest
Posts: n/a
 
      07-27-2003

"Rob" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) ...
> 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]: ");


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

Add the line here:
fflush(stdin);

> 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 */
> }


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.

> }


--
BC


 
Reply With Quote
 
amanayin
Guest
Posts: n/a
 
      07-27-2003
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");
}
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      07-27-2003
In 'comp.lang.c', "Burne C" <(E-Mail Removed)> wrote:

> You can flush the stdin each time to prevent the looping problem.
>
> Add the line here:
> fflush(stdin);
>


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

--
-ed- (E-Mail Removed) [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/
 
Reply With Quote
 
ak
Guest
Posts: n/a
 
      07-27-2003
On Sun, 27 Jul 2003 09:15:44 +0000 (UTC), amanayin <(E-Mail Removed)>
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
 
Reply With Quote
 
Cousin Ricky
Guest
Posts: n/a
 
      07-28-2003
(E-Mail Removed) (Rob) wrote in message news:<(E-Mail Removed) m>...
>
> 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.


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.

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


See <http://www.eskimo.com/~scs/C-faq/q12.19.html>.

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

<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/ ---------------
 
Reply With Quote
 
Neil Cerutti
Guest
Posts: n/a
 
      07-28-2003
In article <(E-Mail Removed)>, Rob
wrote:
> 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.


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
 
Reply With Quote
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      07-29-2003
Groovy hepcat Rob was jivin' on 26 Jul 2003 13:03:17 -0700 in
comp.lang.c.
help with infinite loops and scanf's a cool scene! Dig it!

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


Break the problem down. Divide and conquer. Here are some tips on
how to do that.
First, write a function that reads in a number. Call it getnum() or
something like that. Have it take a string to display as a prompt.
Display this prompt. Then use fgets() to read in a line of input, and
sscanf() or strtol() to scan for a number, checking that proper
numerical input has been entered. If not, then output an error
message, re-display the prompt and try again. If fgets() fails, maybe
you need a way to quit the function and return an error flag. (To
return a number and an error flag, one of these will need to be passed
back via a pointer.) Otherwise, when done, return the number to the
calling function. Also, when using fgets(), remember to check whether
a whole line was read, and take appropriate action if not (ie., read
in and discard the rest of the line).
Next, where your program needs to ask the user to enter a number,
call your getnum() function in a loop, passing an appropriate string
for the prompt. If it returns an error flag, take appropriate steps.
Otherwise, check whether the number you get back is within range, and
keep looping if not, otherwise quit looping.
And then you have your number.

>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,


You can do this with strtol() or sscanf().
Using strtol(), check the endp argument to determine whether there
was any extra (non-numerical) input.
Using sscanf(), use an extra "dummy" conversion specifier (for char)
and argument and check the return value to determine whether the input
contains any non-numeric data. Remember, the return value of sscanf()
indicates the number of items assigned; and in this case you want a
return value of 1. (A return value of 2 means that there are extra
characters after the number, while a return value of 0 means that
there was no number.) So doing the following (after first removing any
newline character from the end of the string) should do what you want:

char dummy;
int num;
int chk;
....
chk = sscanf(input_string, "%d%d", &num, &dummy);
if(1 != chk)
{
/* invalid (non-numerical) input */
}
else
{
/* numerical input */
}

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


Read the FAQ: http://www.eskimo.com/~scs/C-faq/top.html

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ctypes and C Infinite Callback Loops Thomas Dimson Python 1 04-09-2008 10:51 PM
infinite loop with scanf Michael Goerz C Programming 4 10-18-2006 02:52 AM
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
Infinite loops in hashCode() and equals() Mike Schilling Java 11 06-12-2004 04:46 AM



Advertisments