Thread: using while getchar to flush the line

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    using while getchar to flush the line

    if i set up a #define FLUSH while((c=getchar()) != '\n') its quite happy with me using the macro
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define FLUSH while((c=getchar()) != '\n')
    void flush_line(void);
    int main()
    {
        int x = -1, y = 0;
        //char ch;
    
        printf("please enter a number: ");
        scanf(" %1d", &x);
        flush_line();
        printf("please enter a second number: ");
        scanf("%1d", &y);
        printf("\n x = %d and y = %d\n", x, y);
        return 0;
    }
    
    void flush_line(void)
    {
        char c;
        FLUSH;
    }
    however if i delete the char c it doesn't work any more and i get a compiler error saying first time use of variable c.

    Is there a way round this or am i doing something wrong

    many thanks
    coop
    Last edited by cooper1200; 05-07-2019 at 04:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using getchar() to flush newline from input buffer
    By Valour549 in forum C Programming
    Replies: 15
    Last Post: 11-03-2018, 07:49 AM
  2. flush?
    By mp252 in forum C++ Programming
    Replies: 3
    Last Post: 06-22-2013, 10:25 PM
  3. Question on getchar to flush scanf
    By lotrfan in forum C Programming
    Replies: 1
    Last Post: 03-15-2012, 04:59 PM
  4. getchar putchar: a word on each line
    By krazymanrebirth in forum C Programming
    Replies: 27
    Last Post: 09-29-2009, 02:57 PM
  5. Getchar terminating with new line or EOF
    By rocksteady in forum C Programming
    Replies: 1
    Last Post: 10-19-2007, 01:19 PM

Tags for this Thread