 |
16-12-2007, 07:33 PM
|
#1 (permalink)
|
|
Burning Bright
Join Date: May 2006
Location: NIT, Bhopal
Posts: 266
|
Alternate to some imp. "conio.h" functions
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")
__________________
..::Fedora ::.. Freedom + Infinity + Speech
Registered Linux User #447318
GNUger was here.... Grrr....
Maah! Blog
http://brightedges.blogspot.com/
|
|
|
|
Advertisements. Register and be a member of the community to get rid of them.
|
|
Advertisement
|
|
16-12-2007, 07:50 PM
|
#2 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Alternate to some imp. "conio.h" functions
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.
__________________
Harsh J
www.harshj.com
|
|
|
16-12-2007, 07:50 PM
|
#3 (permalink)
|
|
I see right through you.
Join Date: Sep 2005
Location: Chennai
Posts: 597
|
Re: Alternate to some imp. "conio.h" functions
http://www.gidnetwork.com/b-61.html
system("PAUSE") isn't standard either. Use getchar() instead (or cin.get() in c++).
Edit:Beaten...
__________________
I didn't make the world, I only try to live in it.
http://lucentbeing.com
-- Sykora --
|
|
|
16-12-2007, 07:56 PM
|
#4 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Alternate to some imp. "conio.h" functions
Your link's much better though
__________________
Harsh J
www.harshj.com
|
|
|
16-12-2007, 08:16 PM
|
#5 (permalink)
|
|
Burning Bright
Join Date: May 2006
Location: NIT, Bhopal
Posts: 266
|
Re: Alternate to some imp. "conio.h" functions
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
__________________
..::Fedora ::.. Freedom + Infinity + Speech
Registered Linux User #447318
GNUger was here.... Grrr....
Maah! Blog
http://brightedges.blogspot.com/
|
|
|
16-12-2007, 08:28 PM
|
#6 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Alternate to some imp. "conio.h" functions
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.
__________________
Harsh J
www.harshj.com
|
|
|
16-12-2007, 08:56 PM
|
#7 (permalink)
|
|
Burning Bright
Join Date: May 2006
Location: NIT, Bhopal
Posts: 266
|
Re: Alternate to some imp. "conio.h" functions
^^
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.
__________________
..::Fedora ::.. Freedom + Infinity + Speech
Registered Linux User #447318
GNUger was here.... Grrr....
Maah! Blog
http://brightedges.blogspot.com/
|
|
|
16-12-2007, 08:57 PM
|
#8 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Alternate to some imp. "conio.h" functions
ncurses/curses, as I already told above, contains getch() and some other stuff.
__________________
Harsh J
www.harshj.com
|
|
|
16-12-2007, 09:01 PM
|
#9 (permalink)
|
|
Burning Bright
Join Date: May 2006
Location: NIT, Bhopal
Posts: 266
|
Re: Alternate to some imp. "conio.h" functions
Ok I will try ncurses/curses but I wanted something which is strictly based on ANSI C
__________________
..::Fedora ::.. Freedom + Infinity + Speech
Registered Linux User #447318
GNUger was here.... Grrr....
Maah! Blog
http://brightedges.blogspot.com/
|
|
|
16-12-2007, 09:05 PM
|
#10 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Alternate to some imp. "conio.h" functions
How can platform dependent stuff be part of standard code?
__________________
Harsh J
www.harshj.com
|
|
|
16-12-2007, 09:30 PM
|
#11 (permalink)
|
|
Burning Bright
Join Date: May 2006
Location: NIT, Bhopal
Posts: 266
|
Re: Alternate to some imp. "conio.h" functions
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
__________________
..::Fedora ::.. Freedom + Infinity + Speech
Registered Linux User #447318
GNUger was here.... Grrr....
Maah! Blog
http://brightedges.blogspot.com/
Last edited by anantkhaitan; 16-12-2007 at 09:48 PM.
|
|
|
16-12-2007, 10:18 PM
|
#12 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Alternate to some imp. "conio.h" functions
*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;
}
Quote:
|
Originally Posted by anantkhaitan
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?
__________________
Harsh J
www.harshj.com
Last edited by QwertyManiac; 16-12-2007 at 10:20 PM.
Reason: Automerged Doublepost
|
|
|
16-12-2007, 10:35 PM
|
#13 (permalink)
|
|
Burning Bright
Join Date: May 2006
Location: NIT, Bhopal
Posts: 266
|
Re: Alternate to some imp. "conio.h" functions
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:
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...
__________________
..::Fedora ::.. Freedom + Infinity + Speech
Registered Linux User #447318
GNUger was here.... Grrr....
Maah! Blog
http://brightedges.blogspot.com/
|
|
|
16-12-2007, 10:38 PM
|
#14 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Alternate to some imp. "conio.h" functions
Use echo() to turn it back on.
RTF(ull)M
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;
}
__________________
Harsh J
www.harshj.com
Last edited by QwertyManiac; 16-12-2007 at 10:58 PM.
|
|
|
16-12-2007, 11:42 PM
|
#15 (permalink)
|
|
Burning Bright
Join Date: May 2006
Location: NIT, Bhopal
Posts: 266
|
Alternate to some imp. "conio.h" functions
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...
__________________
..::Fedora ::.. Freedom + Infinity + Speech
Registered Linux User #447318
GNUger was here.... Grrr....
Maah! Blog
http://brightedges.blogspot.com/
|
|
|
17-12-2007, 12:54 AM
|
#16 (permalink)
|
|
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
|
Re: Alternate to some imp. "conio.h" functions
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.
__________________
Harsh J
www.harshj.com
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|