Jump to content




Recent Topics

Recent Status Updates

  • Photo
      16 Sep
    Kadence

    Some spammers sign up to CC and then they leave their account hidden so they think it won't get noticed but with an obvious name like "SaxophoneRetailSingapore" it's hard not to know they are a spam bot. #ModLife

    Show comments (3)
  • Photo
      15 Sep
    Error

    Programming is something that I enjoy and want to make a career out of. But, I usually tend to start things and not finish them. Any advice on how I can finish what I start?

    Show comments (1)
  • Photo
      12 Sep
    FacetiousTurtle33

    Just joined. Really enjoy this sight. Excited to become a great programmer, and helper.

    Show comments (3)
View All Updates

Developed by Kemal Taskin
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?