Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
cancel
Showing results for 
Search instead for 
Did you mean: 

getch not working on HP-UX

Highlighted
Orauser
Occasional Visitor

getch not working on HP-UX

Hi Experts,

I need to use getch in my application for masking the password. I am using getch from curses.h.

Below is the my code:

#include
#include

using namespace std;

int main()
{

cout<<"\nBefore getch";

getch();

cout<<"\nafter getch";

return 0;
}

I am compiling using aCC:

aCC -O -mt -AA +Z +eh +DAportable -w -Wc,-ansi_for_scope,off -DUNICODE -D_UNICODE -D_INCLUDE_STDC_A1_SOURCE -D_INCLUDE__STDC_A1_SOURCE -D_UNIX_ -D_REENTRANT -D_POSIX_PTHREAD_SEMANTICS test.cpp -lcurses

It is compiling successfully but when i am running a.out then it is not waiting for the input and just printing the two statements and program ends.

bash-2.04$ a.out

Before getch
after getchbash-2.04$

Please guide me how to make getch() working on HP.

Thanks in advance for your help



6 REPLIES
James R. Ferguson
Acclaimed Contributor

Re: getch not working on HP-UX

Hi:

From the 'getch(3X)' manpages:

int getch(void);

Regards!

...JRF...
Orauser
Occasional Visitor

Re: getch not working on HP-UX

Thanks for your reply James but taking the output of getch in some variable is also not working.

nt main()
{

cout<<"\nBefore getch";

int val = getch();

cout<<"\nafter getch";

return 0;
}
James R. Ferguson
Acclaimed Contributor

Re: getch not working on HP-UX

Hi (again):

I meant to suggest that if you capture the character returned by 'getch()' then you need to output whatever you captured.

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: getch not working on HP-UX

>JRF: I meant to suggest that if you capture the character returned by 'getch()' then you need to output whatever you captured.

I get -1, possibly because?
If in nodelay mode and no data is available, ERR is returned.
#include
#include
int main() {
printf("Before getch:");
int char_in = getch();
printf("\nGot: %.2x|%c\n", (unsigned char)char_in, char_in);
}
Orauser
Occasional Visitor

Re: getch not working on HP-UX

Hi James and Dennis,

Thanks a lot for your timely help!

I am getting below output after running your code snippet that displays the output of getch

bash-2.04$ a.out
Before getch:
Got: ff|ÿ
bash-2.04$

Please suggest me some way on how to fix this. Do i need to set some delay?

Regards
Dennis Handly
Acclaimed Contributor

Re: getch not working on HP-UX

>Please suggest me some way on how to fix this.

You are actually going to have to read the curses documentation. A good starting point would be curses_intro(3X).

I assume you need to tell curses what window or terminal to read from, possibly these:
initscr() or newterm() to initialise Curses and restores these settings when endwin()

>I need to use getch in my application for masking the password.

Perhaps you should toss getch and just turn on and off echoing?