Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

getche function on conio.h library 3

Status
Not open for further replies.

Kornfeld

Programmer
Jun 22, 2001
1
IL
i used to write programs in Visual C++, now i need help in
Unix environment.
when i do include the conio.h library and try to use the getche function, at compile time i get an error message that
the library does not exist.
Should i use another library? or other function?
my programs is a beginner one:

#include <conio.h>
#include <iostream.h>
void main()
{
char name[20],ch;int age;long tel_num;
clrscr();
do
{
cout<<&quot;enter your name,age,and telephone number:&quot;<<endl;
cin>>name>>age>>tel_num ;

cout<<&quot;my name:&quot;<<name<<endl<<&quot;my age:&quot;<<age<<endl<<&quot;my telephone:&quot;<<
tel_num<<endl;
cout<<&quot;press a key to continue&quot;<<endl;
getche();
cout<<&quot;is the information correct?&quot;<<endl;
}while((ch=getch())=='n');
}

thanks in advance
 
It looks to me that most likely if they say no the first time, you are seeing problems. getch will not take care of the end line characters and name will read nothing in on the first cin the second time around and then exit the loop on the number entered for the phone number. Also, you may want to try cin.get() but this will produce the same result. possibly add at the begining of the do while

while(cin.peek() != '\n')
cin.get();

if(cin.peek() == '\n')
cin.get();

Matt

 

There have been similar posts like yours in this & related forums before. AFAIK unix C libraries doesnt provide any function that returns the character from std input on keypress alone . You need to press the line feed(Enter) key also for the character to be read into the buffer. Functions like getchar() operate like this.

You can simulate getch()/getche() in a UNIX environment by using the select/poll methods. You need to keep polling the ioctl of STDIN and select the key press event.

Here is some code , which was posted to an earlier question by rbobbit. This might be helpful for you.
I am posting it here below. It mimics the getch()/kbhit()
functions of conio.h in VC++.

if you want to know how it works, you can try the man pages of select & poll. And the credit for this post goes
to rbobbit, not me :)

Hope this is useful.

Anand


#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <termios.h>




/**********************************************************
* An attempt at mimicking the kbhit() function availableon
* DOS implementations for *nix environments. The terminal
* should have already been set up appropriately.
**********************************************************/
int kbhit(void)
{
fd_set set;
struct timeval tv;
int tmp;
int result;

tv.tv_sec=tv.tv_usec=0; /* select will not wait even a microsecond */

FD_ZERO(&set); /* Must be done to clear the file descriptor set */
FD_SET(0,&set); /* Set watch on stdin (file descriptor 0) */

/* Find out if characters are available in stdin */
tmp=select(1,&set,NULL,NULL,&tv);
if (-1==tmp) {
/* An error occurred with select */
result=-1;
} else {
/* Are there characters available? */
if (FD_ISSET(0,&set)) {
/* Yes */
result=1;
} else {
/* Nope */
result=0;
}
}
return result;
}
int main(void)
{
struct termios new;
struct termios save;
int quit=0;

/* Get the current terminal settings */
if (tcgetattr(0,&save)!=0) {
fputs(&quot;Failed to init&quot;,stderr);
return EXIT_FAILURE;
}

/* We'll use save as a copy to restore the original
* settings later
*/
new=save;

/* Set up terminal so that characters are not
* echoed and turn off canonical mode.
*/
new.c_cc[VMIN]=1;
new.c_lflag&=~ICANON;
new.c_lflag&=~ECHO;
/* Send the new settings to the terminal */
tcsetattr(0,TCSANOW,&new);

puts(&quot;A program that does nothing, hit q or Q to quit&quot;);

while (!quit) {
puts(&quot;waiting...&quot;);
sleep(1);
/* Check to see if the user entered any data */
if (kbhit()==1) {
/* Got some input, just process the 1st character
* You could alternatively process all the user
* input, which would make more sense in most cases
*/
int c=getchar();
switch(toupper((unsigned char)c)) {
case 'Q':
/* User wants to quit, set the flag */
quit=1;
break;
default:
printf(&quot;You entered %c\n&quot;,c);
}
}
/* Do some other processing here */
}
puts(&quot;Quitting...&quot;);
/* Restore the terminal to its original settings */
if (tcsetattr(0,TCSANOW,&save)!=0) {
fputs(&quot;Failed to restore terminal&quot;,stderr);
return EXIT_FAILURE;
}
return 0;
}






~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mail me at abpillai@excite.com if you want additional help. I dont promise that I can solve your problem, but I will try my best.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
conio.h doesn't guarantee its compatibility.
You can use ncurses library on Unix/Linux :
ncurses.h and libncurses.a

Curses library has serveral different versions and
above header and library names are not the same for
all systems. So find the proper library for your system.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top