Alternate to some imp. "conio.h" functions

Status
Not open for further replies.

anantkhaitan

Burning Bright
Guys I want some alternative to getch(), gotoxy(), kbhit(), _setcursortype(), etc. So either recommend me a alternative or help me developing a function of similar kind

Suppose for the program:
Code:
int main()
{
int i=0;
char a[]="Help Me! ",ch;
do
{
ch=getch();
printf("%c",a[i%9]);
i++;
}while(ch!=27); // 27 means ESC
return 0;
}
How will you write the program in ANSI C so that the output program is same/similar.

NOTE: I don't want getch() for pausing between the program .. But for trapping a character.
For pausing I can very well use system("Pause")
 

QwertyManiac

Commander in Chief
getch() is system dependent stuff. To code it would also require usage of system APIs for keyboard input, and so on. This would obviously make it non portable since the way of taking a keyboard input varies with the platform.

Same goes for the rest I think. Most of them are already built for you by the compiler using Windows/DOS APIs, so that you don't have to do the task.

In Linux, there's ncurses/curses libraries which contains getch() and functions similar to the above. But that obviously makes it *nix-dependent, so there we are. Hardware dependency would probably make it non standard? :)

But hey, your code need not be 'standard' always. Its a developer's choice if he wishes to make code standard and portable (mostly a good thing to do), or dependent (easier using the available compiler methods).

So in short, some functions weren't added to standard C/C++ since the methods of their execution varied across platform (Like the unbuffered getch() input). You got to resort to local libraries for them.
 

Sykora

I see right through you.
*www.gidnetwork.com/b-61.html

system("PAUSE") isn't standard either. Use getchar() instead (or cin.get() in c++).

Edit:Beaten...
 
OP
anantkhaitan

anantkhaitan

Burning Bright
Tried both cin.get() and getchar() but the output is not what I actually want..See what my program does is displays array elements one by one irrespective of whatever you type and ends simultaneously when Esc is pressed,
But both the above mentioned functions echoes whatever we type and displays the string after we press Return key.

And sorry guys for starting a new thread ... Because until now I was not knowing about this thread
 

QwertyManiac

Commander in Chief
The fact is, none of todays applications do such a thing. What application asks the user to enter a key to continue these days? DOS is hardly used. Once you get to GUI programming, you won't look for things like these.

Why would you ask the user to keep hitting a key to show information like that? Isn't showing them all and exiting the right thing to do cause the program has no other aim?

You can't hide the echo on some platforms, so there's no way a getch() can be implemented on it.
 
OP
anantkhaitan

anantkhaitan

Burning Bright
^^
This program is just for explaining my purpose..Like in vim editor when you press 'x' instead of echoing it, it deletes the current character.
 
OP
anantkhaitan

anantkhaitan

Burning Bright
Come on, Even the most basic password based programs in *nix systems this kind of function (not getch ofcourse ) .. Just citing examples..
u press your password "abc!@!@!" but screen displays '*' with each character instead of showing the original character itself or shows nothing at all


And tried ncurses 5.6 .. but still the program is not able to stop character from echoing
 
Last edited:

QwertyManiac

Commander in Chief
*nix doesn't show passwords with a *. It doesn't let it echo at all, right?

But I've seen examples of ncurses using echo(), noecho() functions and they worked fine here too!

Try this:
Code:
#include <ncurses.h>

int main()
{    
    initscr();
    printw("No Echo, Hit 3 keys.");
    refresh();  
    noecho();
    getch();
    getch();
    getch();
    endwin();

    return 0;
}
anantkhaitan said:
Come on, Even the most basic password based programs in *nix systems this kind of function (not getch ofcourse ) .. Just citing examples..
u press your password "abc!@!@!" but screen displays '*' with each character instead of showing the original character itself or shows nothing at all


And tried ncurses 5.6 .. but still the program is not able to stop character from echoing
You aren't getting my point. Though all platforms have a method of masking/whatever, the implementation is not the same in each. The OS handles this thing, and thus it varies! So they CANT include it as a standard function, can they?
 
Last edited:
OP
anantkhaitan

anantkhaitan

Burning Bright
Finally .. this noecho() function has done its job.. A very big THANKS to you..
but now if I want to enable the echo effect again, How will I do it??
Plz get me a echo() function ... :p

Try compiling this:
Code:
#include<ncurses.h>
int main()
{
int i=0;
char a[]="Help Me! ",ch;
initscr();
do
{
noecho();
ch=getch();
printf("%c",a[i%9]);
i++;
}while(ch!=27); // 27 means ESC
return 0;
}
Pleaseeee...
 

QwertyManiac

Commander in Chief
Use echo() to turn it back on. :)

RTF(ull)M :p

Code:
#include <ncurses.h>

int main()
{    
    initscr();            
    printw("No Echo, Hit 2 keys. Echo, Hit another 2 keys.");    
    refresh();            
    noecho();
    getch();
    getch();
    echo();
    getch();
    getch();            
    endwin();

    return 0;
}
printf is not for ncurses ... use printw. This one works fine.
Code:
#include<ncurses.h>

int main()
{
    int i=0;
    char a[]="Help Me! ",ch;
    initscr();
    
    do
    {
        noecho();
        ch=getch();
        printw("%c",a[i%9]);
        i++;
    }while(ch!=27); // 27 means ESC
    
    endwin();
    return 0;
}
 
Last edited:
OP
anantkhaitan

anantkhaitan

Burning Bright
Superb man.. thanks for that..Its finally working.. thanks for every help.. I am so happy..
now you must be knowing what program I was talking about.... thanks again...
 

QwertyManiac

Commander in Chief
I knew what your program did already, I was just pointing out, and still do, about how un-useful it is to make such stuff up. TUI's mostly a thing of past now.

Though it is faster to use than GUI sometimes, it just doesn't get its needed popularity.
 
Status
Not open for further replies.
Top Bottom