Thread: Problems with input and fflush

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    13

    Problems with input and fflush

    Hi,

    I'm coding in Linux, and I found problems getting input. I can't make fflush() work, so often my scanf, getchar, getc... get a char from previous input.

    so something like this

    /*.... */

    fflush(stdin)
    scanf("%c", &c);

    switch(c)
    {
    /*.... */

    often runs without leting you write your char.

    any idea?


    For instance, this program doesn't work properly

    #include <stdio.h>
    int main(int argc, char **argv)
    {
    char c;

    while(1)
    {
    printf("Press a key... \n");
    fflush(stdin);
    scanf("%c", &c);
    switch(c)
    {
    case 'a':
    printf("pressed %c\n\n", c);
    break;
    case 'b':
    printf("pressed %c\n\n", c);
    break;
    case 'c':
    printf("pressed %c\n\n", c);
    break;
    case 'd':
    printf("pressed %c\n\n", c);
    break;
    default:
    printf("No key from a to d pressed n\n");
    break;
    }

    }
    }


    Thanx in advance.
    Last edited by edugarcia; 11-24-2004 at 01:57 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351
    Stick to using fgets(), then you don't have to worry about the mess scanf() makes of the input stream, and the need to try and flush the input stream goes away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. flushall()
    By salvadoravi in forum C Programming
    Replies: 21
    Last Post: 12-24-2007, 12:39 PM
  2. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM
  3. Message printing problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 05:05 AM
  4. clearing input buffert.
    By Shogun in forum C Programming
    Replies: 5
    Last Post: 06-07-2003, 01:35 PM
  5. why is the input not working
    By mackol in forum C Programming
    Replies: 7
    Last Post: 11-07-2002, 04:28 AM