Problems with input and fflush

This is a discussion on Problems with input and fflush within the Linux Programming forums, part of the Platform Specific Boards category; Hi, I'm coding in Linux, and I found problems getting input. I can't make fflush() work, so often my scanf, ...

  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 12:57 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    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, 11:39 AM
  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, 03:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21