Thread: switch problem

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    77

    switch problem

    Code:
    #include <stdio.h>
    
    
    main(void)
    {
    	int choice = 0;
    
    	while(choice != 4)
    	{
    		printf("key in a value");
    		scanf("%d", &choice);
    
    		if (choice >= 1 && choice <= 3)
    
    			switch(choice)
    		{
    
    			case 1:
    				printf("yes");
    				break;
    
    			case 2:
    				printf("double yes");
    				break;
    
    			case 3:
    				printf("trileple yes");
    				break;
    
    			case 4:
    				printf("byebye");
    				break;
    			default:
    				printf("wtf,type again");
    	}
    	}
    
    }
    how come when i key in any value other than 1,2,3,4

    the while loop keep repeat infinite(the key in a value keep repeat n repeat n repeat)

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you mean other integer or some junk?

    if you enter junk - scanf will leave it in the input stream

    you will need to get rid of it - there are a lot of samples on the forum, and even in tthe FAQ as well (look for something about why not to use fflush(stdin) )
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by vart View Post
    you mean other integer or some junk?

    if you enter junk - scanf will leave it in the input stream

    you will need to get rid of it - there are a lot of samples on the forum, and even in tthe FAQ as well (look for something about why not to use fflush(stdin) )
    yea key in some junk how should i get clear of that.. i dont know what 2 do now
    Last edited by archriku; 03-28-2009 at 04:14 AM.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by archriku View Post
    yea but my programming book said if other value other then 1234 keyed in
    the while loop repeat and will ask me to key in other value.. not key in other value(infinite times)
    It will. If you enter 42 or 9, it should loop again. If you enter something that isn't a number (or part number, but parts not numeric, such as 123abc), then the "non-numbers" will be stuck in the input stream, because scanf() will leave anything that isn't a number in the input buffer - and there's nothing else to read the non-number out of there, so it just loops (very quickly) until you force the application to exit.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by matsp View Post
    It will. If you enter 42 or 9, it should loop again. If you enter something that isn't a number (or part number, but parts not numeric, such as 123abc), then the "non-numbers" will be stuck in the input stream, because scanf() will leave anything that isn't a number in the input buffer - and there's nothing else to read the non-number out of there, so it just loops (very quickly) until you force the application to exit.

    --
    Mats
    oh can this be done? if i key in junk value.. the while loop repeat again without infinite types.. like as if i key in a integer value

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Of course it can be done. But we can't rely on scanf to do it for us.

    As vart suggests, you have to get rid of the junk.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by matsp View Post
    Of course it can be done. But we can't rely on scanf to do it for us.

    As vart suggests, you have to get rid of the junk.

    --
    Mats
    scanf("%d,& num");
    fflush(stdin);

    i just read this example.. izzit means it flush away stdin? so i just add this line below my scanf?

    i haven learn this fflush .. can roughly explain for me? what it means flush? throw away thejunk?so if it is throwing the junk(a) which i input what happen to my while loop?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    fflush(stdin) is non-standard and undefined, which means that it can do anything between "what you want" through "nothing at all" to "launch intercontinental missiles" when you use it. There is a FAQ entry to tell you how to clear the input buffer.

    fflush is only defined for output files, and stdin is an input file, not an output file (assuming you haven't done REALLY strange things [1] - in which case scanf() won't work anyways)

    [1] For example:
    Code:
       fclose(stdin);
       stdin = fopen("lala.txt", "w");
    Now, if you do that, things will behave VERY strange if you try to use any input functions that use stdin. Do not do this!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    fflush(stdin) is not a particularly good idea. It yields undefined behaviour.

    By "get rid of the junk" vart and matsp mean that your code needs to be written to interpret the input in order to unambiguously extract what you need from it. A side effect of doing that is that unnecessary characters (i.e. junk) in the input will be discarded.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I see you still too lazy to click on the FAQ link...

    FAQ > How do I... (Level 2) > Flush the input buffer
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by grumpy View Post
    fflush(stdin) is not a particularly good idea. It yields undefined behaviour.

    By "get rid of the junk" vart and matsp mean that your code needs to be written to interpret the input in order to unambiguously extract what you need from it. A side effect of doing that is that unnecessary characters (i.e. junk) in the input will be discarded.
    how do i get rid of it?use the flush thing?if not what i use?

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by archriku View Post
    how do i get rid of it?use the flush thing?if not what i use?
    read the link in post #10
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by vart View Post
    read the link in post #10
    what link ?n what is post #10?

    u mean page 10 with topic "link"?i cant find it

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by archriku View Post
    how do i get rid of it?use the flush thing?if not what i use?
    There are a few different options:
    1. You rewrite your input code to read a string, and then attempt to convert the string to an integer value, using for example sscanf(). Remember to check the result of the conversions - if it went wrong, you should say something like "that's not a number".
    2. You add a function/loop to flush it. See the FAQ link above.
    3. Write your own low-level read function that accepts keyboard input without showing it on the screen, and filters out junk before it ever gets into the rest of the program (e.g we may pass a string to the function with acceptable characters, in this case 1,2,3,4 would be OK input, the rest should be discarded).

    1 and 2 are relatively easy to do. 3 is the "proper" way for commercial, robust applications, but it's also quite hard work, particularly if it's supposed to work on many different types of machines.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by archriku View Post
    what link ?n what is post #10?

    u mean page 10 with topic "link"?i cant find it
    If you look at the right-hand side of the blue line at the top of each post, there is a #nn number - this is the "post within this thread".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM
  2. problem on switch
    By toxicherry in forum C Programming
    Replies: 11
    Last Post: 12-31-2007, 05:17 AM
  3. Switch Problem
    By Tynnhammar in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2004, 11:57 AM
  4. Replies: 1
    Last Post: 08-31-2004, 04:07 AM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM