Thread: Getch Refresh? Simple Problems with Getch()

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    22

    Getch Refresh? Simple Problems with Getch()

    I use DevC++
    and I have 2 problems

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    main()
    {
          int x = getch();
          
          printf("1st getch = %d",x);
          getch();
          printf("2nd getch = %d",x);
          getch(); 
          printf("3rd getch = %d",x);
          
    }
    First Problem: When I declare x = getch(); the getch there is not excluded, I mean you still need to put a value to go to the next statement. Is it possible skipping it in declaring for a variable?

    Second Problem: The value of the my first getch() will be the value of all getch, I need a way to refresh it or delete the value of it.
    For Example: I press Enter
    Ascii Value of Enter is 13
    Output would be:

    1st Getch = 13
    2nd Getch = 13 //I press other keys
    3rd Getch = 13 //I press a different key

    What I want is like
    Output:

    1st Getch = 13
    2nd Getch = ## //The Ascii Value of another key
    3rd Getch = ## // The Ascii Value of another key

    Please Help . I'm trying to solve this for 2 hours now, I tried fflush(stdin) but it wont work.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you want to keep track of the key that getch is returning, you need to assign it to something. The line where you declare a variable doesn't magically make it refresh every time getch() is called.
    Code:
    int first, second, third;
    
    first = getch();
    second = getch();
    third = getch();
    Assigns three different return calls to the appropriate variables.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    But Wait. If I did that the declaration of getch in the variables will not be excluded

    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    main()
    {
          int x = getch(),y = getch(),z = getch();
          
          getch();
          printf("1st getch = %d",x);
          getch();
          printf("2nd getch = %d",y);
          getch(); 
          printf("3rd getch = %d",z);
          getch();
    }
    Here you will see I want is, before the printf It will ask the getch()
    Before printf First Getch, that value of getch I want for my printf First Getch
    Not the vualue in my declarations

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include<stdio.h>
    #include<conio.h>
    
    
    main()
    {
          int x = getch();
          
          printf("1st getch = %d",x);
          x = getch();
          printf("2nd getch = %d",x);
          x = getch(); 
          printf("3rd getch = %d",x);
          
    }
    And you should be using getchar() not getch()

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    If I use getchar() I need to press Enter to execute it

    Ok my project is like object oriented or a cheat way of having one

    Like If I press left key the left box will have a border and if I press enter it will go to a function vice versa to the right

    I use getch() because I don't need to press Enter when I input a key stroke

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... but you still need to capture the return value of the call *every single time*.

    Code:
    while (1)
      {   switch( getch() )
           {
               case '1' :
                   printf("Function #1");
                   break;
               case '2' :
                   printf("Function #2");
                   break;
    
               // and more cases as needed
    
               case 'Q' :
                  printf("Bye Bye For Now!);
                  exit(0);
              default :
                  printf("Unrecognized entry"); } }
    And... what you are writing is not even close to object oriented sillyness ... er, I mean "code"... what you are trying to emulate is a windows message queue... and in that case why not use the real deal?

    http://www.winprog.org/tutorial/
    Last edited by CommonTater; 10-26-2011 at 12:30 AM.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    Ok but How will I put it Arrow Keys?
    Your solution is great but my problem now is arrow keys

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    22
    Damn I got it, Thanks dude, I found a way. I convert it to be a int

    New Problem

    Arrow Keys give two values(I think) cause It activate Case 224 and Default
    Both are showing up
    Last edited by justin777; 10-26-2011 at 01:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a simple getch question
    By fizisyen in forum C++ Programming
    Replies: 6
    Last Post: 11-15-2003, 12:12 PM
  2. simple question -getch \n and \r
    By GanglyLamb in forum C Programming
    Replies: 1
    Last Post: 06-10-2003, 08:12 AM
  3. problems with getch()
    By Waldo2k2 in forum C Programming
    Replies: 3
    Last Post: 01-21-2003, 07:10 AM
  4. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM
  5. Problems with Getch() program execution order
    By napkin111 in forum C++ Programming
    Replies: 4
    Last Post: 05-08-2002, 01:44 PM