# Alternate to some imp. "conio.h" functions



## anantkhaitan (Dec 16, 2007)

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:

```
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)

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 (Dec 16, 2007)

*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)

Your link's much better though


----------



## anantkhaitan (Dec 16, 2007)

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)

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)

^^
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)

ncurses/curses, as I already told above, contains getch() and some other stuff.


----------



## anantkhaitan (Dec 16, 2007)

Ok I will try ncurses/curses but I wanted something which is strictly based on ANSI C


----------



## QwertyManiac (Dec 16, 2007)

How can platform dependent stuff be part of standard code?


----------



## anantkhaitan (Dec 16, 2007)

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)

*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:

```
#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)

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 ... 

Try compiling this:

```
#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)

Use echo() to turn it back on. 

RTF(ull)M 


```
#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.

```
#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)

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)

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.


----------

