Menu

Single keypress using getche() or getchar()

2004-05-20
2012-09-26
  • Nobody/Anonymous

    Single keypress using C++ getche(), getch() or cin.get() and C getchar() all work fine if coded in the function main().  However, if they are coded in a user defined function then in addition to pressing a key, the return key has to be pressed. 
    Any ideas?

    Tony

     
    • Nobody/Anonymous

      Tony,
      You must be doing something wrong. I cannot figure out what it might be, but I am currently developing a game, and I use getch() nearly all the time...
      Give a little more info, preferably some code.
      Yours sincerely,
      Peter.
      TDLGames Programmer.
      Visit our website at
      http://www.tdlgames.com

       
    • Nobody/Anonymous

      Peter

      I have been trying various things but still cannot get it to work.  In fact getch() causes an implicit declaration error.  I am using version 4 as version 5 seems to be in various stages of beta.  The code below works correctly using Borland 4.52 I simply need to press the appropriate key, but I have to press the return key with Dev-CPP in both the functions main() and getKey().

      Many thanks
      Tony

      #include<iostream.h>
      #include<conio.h>

      //function to get a single character
      char getKey();

      void main()
      {//start of function main
        char whichKey;

        whichKey=getKey();
        clrscr();
        cout<<"The key pressed was "<<whichKey;
        cout<<"\n\nPress any key... ";

        cin.ignore();
        whichKey=getche();
      }//end of function main

      /***************************************************\ The function getKey validates for values A, B, C, D
      \***************************************************/
      char getKey()
      {//start of function getKey
        char keyPressed;
        clrscr();

        do
        {//start of do loop
          cout<<"Press a key between A..D ";
          keyPressed=getche();
          if(keyPressed>'D')
            keyPressed-=32;
        }while(keyPressed<'A'||keyPressed>'D');
        //keep looping until a valid key is pressed

        return keyPressed;
      }//end of function getKey

       
    • Nobody/Anonymous

      Tony,
      I have tried to fix your problem.
      I include the code at the bottom of this post. I made a few minimal changes to the code.
      Also please note that both clrscr() and getch() are Borland specific functions, although GCC supports them.
      I hope this helps you.
      Yours sincerely,
      Peter.
      TDLGames Programmer.
      Visit our website at
      http://www.tdlgames.com
      [Code]
      #include<iostream>
      #include<conio.h>
      using namespace std;
      //function to get a single character
      char getKey();

      int main()
      {//start of function main
      char whichKey;

      whichKey=getKey();
      system("cls");
      cout<<"The key pressed was "<<whichKey;
      cout<<"\n\nPress any key... ";

      // cin.ignore();
      whichKey=getche();
      }//end of function main

      /***************************************************\ The function getKey validates for values A, B, C, D
      \***************************************************/
      char getKey()
      {//start of function getKey
      char keyPressed;
      system("cls");

      do
      {//start of do loop
      cout<<"Press a key between A..D ";
      keyPressed=getche();
      if(keyPressed>'D')
      keyPressed-=32;
      }while(keyPressed<'A'||keyPressed>'D');
      //keep looping until a valid key is pressed

      return keyPressed;
      }//end of function getKey
      [/Code]

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.