a probelm about switch

Discussion in 'C Programming' started by QQ, May 17, 2005.

  1. QQ

    QQ Guest

    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!
    }
     
    QQ, May 17, 2005
    #1
    1. Advertising

  2. QQ

    Michael Mair Guest

    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.
     
    Michael Mair, May 17, 2005
    #2
    1. Advertising

  3. QQ

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, May 17, 2005
    #3
  4. QQ

    -berlin.de Guest

    QQ <> 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 ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, May 17, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    1,031
    David Binnie
    Mar 17, 2006
  2. Grey
    Replies:
    0
    Views:
    305
  3. Benjamin Bittner

    Probelm with embed server controls

    Benjamin Bittner, Jul 7, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    2,543
    Benjamin Bittner
    Jul 7, 2004
  4. Mohamed Zaki

    Dropdown viewstate probelm

    Mohamed Zaki, Jul 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    418
    Mohamed Zaki
    Jul 13, 2004
  5. Switch Within A Switch

    , Apr 22, 2006, in forum: Javascript
    Replies:
    7
    Views:
    116
    Lasse Reichstein Nielsen
    Apr 22, 2006
Loading...

Share This Page