a probelm about switch

Q

QQ

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!
}
 
M

Michael Mair

QQ said:
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

Eric Sosman

QQ said:
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.
 
J

Jens.Toerring

QQ said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top