Jump to content




Recent Status Updates

View All Updates

Developed by TechBiz Xccelerator
Photo
- - - - -

conio.h, getch(), getche()


  • Please log in to reply
3 replies to this topic

#1 jackson6612

jackson6612

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 285 posts

Posted 19 May 2011 - 02:46 PM

Hi :)

I found the following code (which I've changed a bit) and the explanation in blue in a book. I don't understand the use of getch() or other function getche() the book refers to. Could you please comment on this? And what does "\r" stands for? Thanks for all the help.

One function in EXTERN, getachar(), reads characters from the keyboard. It uses the library function getch(), which is like getche() except that it doesn’t echo the character typed to the screen (hence the absence of the final e in the name). A second EXTERN function, putachar(), displays each character on the screen. The effect is that what you type is displayed in the normal way:
I’m typing in this line of text


// extern.cpp
// demonstrates global variables

#include <iostream>
#include <cstdlib>
#include <conio.h>        //for getch()

using namespace std;

char ch = 'a';            //global variable ch

void getachar();          //function declarations
void putachar();

int main()
   {
       cout << "ch is: " << ch << endl;

   while( ch != [B]'\r'[/B] )    //main() accesses ch
      {
      getachar();
      putachar();
      }

   cout << endl;

   system("pause");

   return 0;
   }

//--------------------------------------------------------------
void getachar()           //getachar() accesses ch
   {
   ch = getch();
   }
//--------------------------------------------------------------
void putachar()           //putachar() accesses ch
   {
   cout << ch;
   }

  • 0
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2 mebob

mebob

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 19 May 2011 - 03:54 PM

Both getch() and getche() grab the first key input after the function is called and returns it. getche() shows the character you typed on the console screen, getch() does not. "\r" is something called a "carriage return". In Windows and DOS, unless using the standard libraries, to make a new line, you need to do a carriage return THEN a newline like this: "\r\n". Basically, when '\r' is being used in that code, it waits for the enter key to be pressed.
  • 0
Latinamne loqueris?

#3 gdjs

gdjs

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 19 May 2011 - 07:27 PM

getch() and getche() are not part of the C Standard.
  • 0

#4 mebob

mebob

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 467 posts
  • Programming Language:C, C++, Assembly
  • Learning:PHP

Posted 20 May 2011 - 11:26 AM

Programming courses often use the non-standard conio.h functions.
  • 0
Latinamne loqueris?




Powered by binpress