Thread: getch() getchar() no need 4 fflush?

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    getch() getchar() no need 4 fflush?

    Hi Im new here and just learning C. Im using VTC video tutorials. The guy on it is saying that I have to put in fflush(stdin); to keep the window open so it dosent automatically close.
    here is the code I was supposed to put it into.

    Code:
    main()
    {
          short age;
          printf ("How old are you: ");
          scanf ("%d", &age);
          printf ("The age you have entered is %d\n", age);
          getchar();
          
    }
    I was supposed to put it above getchar();
    But I put getch(); and it didnt close the window, so is getch();
    an exceptable replacement for using fflush(stdin); ?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > The guy on it is saying that I have to put in fflush(stdin);
    Great, another "teacher" selling snake oil.

    Read
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    Also
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385


    Your scanf() call is wrong as well, %d is for reading ints, not shorts.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    No its not that I dont trust him. I saw someone using getch();
    So I thought it was short for getchar(); and tried using it and the window stayed up. When I used getchar(); the window closed. Also the short was int I was just trying something else there.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    I just thought I found another way to do the same thing is all. :\

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    getch() is specific to your compiler
    getchar() is standard.

    The two 'streams' do not have to be synchronised, so doing
    c1 = getchar();
    c2 = getch();
    Is not guaranteed to produce the effect of making c1='h' and c2='e' if you type in "hello"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 05:15 AM
  3. getch() vs. getchar()
    By mako in forum C Programming
    Replies: 5
    Last Post: 01-24-2006, 11:20 AM
  4. Difference between getch() and getchar()
    By codec in forum C Programming
    Replies: 4
    Last Post: 04-04-2004, 02:34 AM
  5. Can anybody take a look at this?
    By TerryBogard in forum C Programming
    Replies: 10
    Last Post: 11-21-2002, 01:11 PM