DIGIT-ARC read-only archive · snapshot: July 23, 2026

Alternate to some imp. "conio.h" functions

16 posts · originally from https://geek.digit.in/community/threads/alternate-to-some-imp-conio-h-functions.75895/

anantkhaitan
anantkhaitan Original post #1
Dec 16, 2007 at 7:33 PM
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
Dec 16, 2007 at 7:50 PM
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
Sykora #3
Dec 16, 2007 at 7:50 PM
*www.gidnetwork.com/b-61.html

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

Edit:Beaten...
QwertyManiac
Dec 16, 2007 at 7:56 PM
Your link's much better though :p
anantkhaitan
Dec 16, 2007 at 8:16 PM
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
Dec 16, 2007 at 8:28 PM
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.
anantkhaitan
Dec 16, 2007 at 8:56 PM
^^
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.
QwertyManiac
Dec 16, 2007 at 8:57 PM
ncurses/curses, as I already told above, contains getch() and some other stuff.
anantkhaitan
Dec 16, 2007 at 9:01 PM
Ok I will try ncurses/curses but I wanted something which is strictly based on ANSI C
QwertyManiac
Dec 16, 2007 at 9:05 PM
How can platform dependent stuff be part of standard code? :?
anantkhaitan
Dec 16, 2007 at 9:30 PM
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
QwertyManiac
Dec 16, 2007 at 10:18 PM
*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?
anantkhaitan
Dec 16, 2007 at 10:35 PM
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
Dec 16, 2007 at 10:38 PM
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;
}
anantkhaitan
Dec 16, 2007 at 11:42 PM
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
Dec 17, 2007 at 12:54 AM
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.