Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > a probelm about switch

Reply
Thread Tools

a probelm about switch

 
 
QQ
Guest
Posts: n/a
 
      05-17-2005
Hi here is my short program

void main()
{
while(1)
{
int command;
scanf("%d", &command);
switch(command)
{
case 99:
{
printf("Exit............\n");
exit(1);
}
case 0:
printf(" case 1\n");
break;
case 1:/
printf(" case 1\n");
break;
default:
printf("Please choose between 0 and 10\n");
}
}

Now when I run this program
if I input a b c or anything other than 0,1,
it will go to inifinite loop
what's the problem?
Thanks a lot!
}

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      05-17-2005
QQ wrote:
> Hi here is my short program
>
> void main()
> {
> while(1)
> {
> int command;
> scanf("%d", &command);
> switch(command)
> {
> case 99:
> {
> printf("Exit............\n");
> exit(1);
> }
> case 0:
> printf(" case 1\n");
> break;
> case 1:/
> printf(" case 1\n");
> break;
> default:
> printf("Please choose between 0 and 10\n");
> }
> }
>
> Now when I run this program
> if I input a b c or anything other than 0,1,
> it will go to inifinite loop
> what's the problem?
> Thanks a lot!


Read the comp.lang.c FAQ 12.19; 12.20, 12.18a and 12.17 may help you
to get a deeper understanding. Start from here:

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


Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      05-17-2005


QQ wrote:
> Hi here is my short program
>
> void main()
> {
> while(1)
> {
> int command;
> scanf("%d", &command);
> switch(command)
> {
> case 99:
> {
> printf("Exit............\n");
> exit(1);
> }
> case 0:
> printf(" case 1\n");
> break;
> case 1:/
> printf(" case 1\n");
> break;
> default:
> printf("Please choose between 0 and 10\n");
> }
> }
>
> Now when I run this program
> if I input a b c or anything other than 0,1,
> it will go to inifinite loop
> what's the problem?


This is Question 12.19 in the comp.lang.c Frequently
Asked Questions (FAQ) list

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

Your program has a few other problems, too:

- You must #include <stdio.h> if you use scanf() or printf(),
and you must #include <stdlib.h> if you use exit(). (Well,
there *are* ways to do without them, but they're all harder
than simply writing the #include's to begin with.)

- The main() function must return an `int' value.

- You aren't checking whether the scanf() call succeeded
or failed.

- exit(1) may not mean the same thing on all systems; use
exit(0) or exit(EXIT_SUCCESS) or exit(EXIT_FAILURE) if you
care about portability.

- You may want to reconsider what output you produce in
case 0.

- You have a syntax error at case 1.

- The end of the main() function has been chopped off.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      05-17-2005
QQ <(E-Mail Removed)> wrote:
> Hi here is my short program


> void main()


Make that

int main( void )

since main() must return an integer.

> {
> while(1)
> {
> int command;
> scanf("%d", &command);
> switch(command)
> {
> case 99:
> {
> printf("Exit............\n");
> exit(1);
> }
> case 0:
> printf(" case 1\n");
> break;
> case 1:/
> printf(" case 1\n");
> break;
> default:
> printf("Please choose between 0 and 10\n");
> }
> }


> Now when I run this program
> if I input a b c or anything other than 0,1,


Actually, any integer should do, not just 0 and 1...

> it will go to inifinite loop
> what's the problem?


The direct problem is that you don't check the return value of scanf().
You tell it to read an integer but when there isn't one it returns 0 to
tell you that nothing has been read (scanf() always returns the number
of items it read). Since scanf() couldn't read anything everything is
still in the input buffer and when you call scanf() again it fails again
since there's still nothing in the input buffer that would match the
required integer. And with your infinite loop you only get out of that
by killing the program.

What you may learn from this is that scanf() is extremely difficult
(if possible at all) to use with user input. The strategy usually
recommended is to use e.g. fgets() to read in a whole line of input
and inspect this for what you want. Then you can discard input that
you can't deal with and, since it then doesn't stick in the input
buffer anymore, you can continue with reading the next line instead
of failing at the same piece of input over and over again.

In your simple case it might be good enough to just check the return
value of scanf() and, if it's 0, complain to the user _and_ read in
the whole offending line with fgets() to get rid of it in order to
keep scanf() chocking on it.
Regards, Jens
--
\ Jens Thoms Toerring ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
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
Mozilla browser probelm - Pls help kimshapiro100@yahoo.com Firefox 3 03-09-2006 07:10 PM
wireless probelm =?Utf-8?B?YWFyb25zaW1v?= Wireless Networking 1 10-24-2005 09:35 PM
Linksys Probelm Jason Wireless Networking 1 11-24-2004 12:16 AM
PIX Probelm Greg Cisco 3 05-21-2004 02:16 PM
Cisco 1751 Point-to-point probelm. Tom Cisco 2 05-21-2004 01:37 PM



Advertisments