Thread: getch() acting up...

  1. #1
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823

    getch() acting up...

    OK, I put together a little app to show how pointers work, to be uploaded to my website. However, getch() doesn't seem to be stopping the program unless I have two of them. It acts the same no matter where the first one is, but the 2nd one has to be just before return.

    Using Dev-C++ on NT 4.

    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    
    void foo(int *y);
    
    int main(int argc, char *argv[])
    {
      int x = 100;
      int *y = 0;
      y = &x;
      printf("Enter your number: ");
      scanf("%d",&x);
      printf("The value of x is: %d \n",x);
      printf("The address if x is: %d \n",&x);
      printf("The value of y is: %d \n",y);
      printf("The value at that address is: %d \n",*y);
      getch();
      foo(y);
      printf("The new value of x is: %d \n",x);
      printf("The address if x is: %d \n",&x);
      printf("The value of y is: %d \n",y);
      printf("The value at that address is: %d \n",*y);
      getch();
      return 0;
    }
    
    void foo(int *y)
    {
     *y += 125;
    }

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    I think (but am not sure) that the scanf may be leaving the \n in the stream which the getch reads as a keypress. Try adding a fflush(STDIN) after the scanf.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    Thnx, s_c, it worked (once I changed STDIN to stdin )

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    fflush(stdin); is an undefined operation - so it works for you, but it doesn't work for me.

    If you want to avoid all the mess which scanf leaves behind, then do this.

    Code:
    char buff[BUFSIZ];
    fgets( buff, BUFSIZ, stdin );
    sscanf( buff, "%d", &x );
    So it's a bit longer, but at least you know where you are in the input stream.

    And it's a lot easier to recover from errors, you might expect someone to type in "42", but if they type in "fourty-two", simple scanf calls are in a world of pain.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Unregistered
    Guest
    why is that undefined salem?

    is stdin not a file?

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    mmmm autologging in not working prperly now!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    On DOS yes fflush(stdin) may "work", your already
    using getch so your not going anywhere but
    dos and borland's compiler. Ofcourse "work"
    is really up to whoever's implementing it as
    it's undefined by the standard.

    It doesn't work on linux, if you absoutly must get some out of the input stream you can do

    Code:
    int c;
    while((c = getc(stdin)) != EOF)
           ;
    Your best off just using fgets.

  8. #8
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Before someone notices ...

    Code:
    int c;
    while((c = getc(stdin)) != EOF && c != '\n')
           ;

  9. #9
    Unregistered
    Guest
    if i remember correctly this should handle clean up after scanf in the same function call

    scanf("%d %*c", &x);

    the %*c will read in '\n' but skip the arguement so it won't cause any problems.

  10. #10
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,263
    why does it keep loggin me out???

    the above unregistered post is mine...

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. Difference between getch() and getchar()
    By codec in forum C Programming
    Replies: 4
    Last Post: 04-04-2004, 02:34 AM
  4. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  5. Using getch()
    By pr0ficient in forum Linux Programming
    Replies: 2
    Last Post: 07-26-2002, 05:59 AM