Thread: switch problem

  1. #16
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    #include <stdio.h>


    int main(void)
    {
    int ch;
    char buf[BUFSIZ];

    puts("Flushing input");

    while ((ch = getchar()) != '\n' && ch != EOF);

    printf ("Enter some text: ");

    if (fgets(buf, sizeof(buf), stdin))
    {
    printf ("You entered: %s", buf);
    }
    return 0;
    }


    got few noob question, what does the
    Code:
    puts("Flushing input");
      
      while ((ch = getchar()) != '\n' && ch != EOF);
    what it flushed?and why it need to be flushed?

    if (fgets(buf, sizeof(buf), stdin))<=== the stdin means what?

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    flushing input means just that - removing anything in the input buffer. There is a slight flaw in this particular piece of code in that it will wait for the user to type enter if there isn't something in the input already.

    Edit: stdin is the "standard input", which normally means your keyboard/console input channel.

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

  3. #18
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    Quote Originally Posted by matsp View Post
    flushing input means just that - removing anything in the input buffer. There is a slight flaw in this particular piece of code in that it will wait for the user to type enter if there isn't something in the input already.

    Edit: stdin is the "standard input", which normally means your keyboard/console input channel.

    --
    Mats
    ok i try the "How to convert char to int tutorial"

    #include <stdio.h>


    Code:
    main(void)
    {
    	int choice = 0;
    
    	while(choice != 4)
    	{
    		printf("key in a value");
    		
    		choice = getchar();
    
    
    
    		
    			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");
    	}
    	}
    
    }
    how come when i enter 1,2,3,4 its nvr show according to the cases

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Because the character 1 is not the integer value 1 - it is 49 in ASCII and some other number in EBCDIC (ASCII and EBCDIC are the two most common encodings of characters).

    Since it may not always be 49 (and it's hard to know what 49 means anyways), we can use the compiler to do the translation by enclosign the 1 in single quotes: '1' - it translates to the correct encoding for the character 1 in your system.

    --
    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. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Stdin usually refers to the input stream from the keyboard however, that input stream can be redirected - say to a file, for instance.

    When you scanf("%c") for a char, the char is pulled off the keyboard buffer, by scanf, but the "enter" key will leave behind it's newline ('\n'), char, in the keyboard buffer. If a subsequent scanf("%c") is in your code, scanf() will see the newline char in the buffer, and take it as it's designated input, not waiting for any more input from the user. It has the effect of making it seem like the scanf() line of code was somehow "skipped" over.

    If you change your scanf() calls for a single char, from this: scanf("%c"), to this scanf(" %c") (just adding that one space before the % sign), the "skipping" will be greatly reduced.

  6. #21
    Registered User
    Join Date
    Mar 2009
    Posts
    77
    lolx so back to the question.. what should i do to make the while loop run once only when the user key in a junk ?

    what method should i go for with my noob skills?

    i read the flush things.. it just the away the making "123456acb" to "1234" somthing like that.. so how i do use this flush things into my code?

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't want the while loop to run only once if the user keys in junk. If the user keys in junk 20 times, you want it to loop 21 times asking for their choice.

    I'd put that flush code, right at the very bottom of the while loop, just before it loops back up to get input again, if needed.

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