PDA

View Full Version : getch()


H1234
08-12-2003, 11:15 AM
I was wondering what arguments can be put into the parenthesis of this function. Can I put a string, making the program exit only on the imput of that string/character?

Smerdyakov
08-12-2003, 11:48 AM
Why don't you look in the online help of your compiler and see?

(Man pages in UNIX, press F1 in Windows IDE's, etc.)

Jamaican
09-12-2003, 03:38 PM
Yes you can, however, not in the format you are thinking of: its not a regurlar function: you don't pass arguments to this function, it automaticaly takes the input from the user (keyboard as you press the keys) and you don't have to press Enter.
you will not see the Keyboard input, though
if you want to see it you can use the function "getche()", it acts the same way as getch() (the 'e' at the end is the echo, it echoes back the input to the screen)

(I don't know what I.D.E. you're using, but I'm using Dev-C++ so)

this s what you might do
include the "conio.h" header file

#include <iostream>
#include <conio.h>
using namespace std;
//////////////


int main() // in MS VC++ you'd put, "void main (void)"
{
char ch ='a';

ch = getch();
if (ch == 'd') // 'd' would be the password, more to the point, the pass letter
{
cout << "you are now logged in" ;
}
system( "pause" );
return 0;
}


(the system("pause") function is just to stop the console window from closing, don't add it in MS VC++.

The above is more explanetory

you could have done this:

#include <iostream>
#include <conio.h>
using namespace std;
//////////////


int main()
{

if (getch() == 'd')
{
cout << "you are now logged in" ;
}

system( "pause" );
return 0;
}

more compact and I love this way
you could also put it in a loop

Jamaican
09-12-2003, 03:57 PM
Another thing, the "conio.h" header file is very usfull, go look it up, trust me this header file comes in handy "getche" and "getch()" is just two of its many functions