English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I wrote the following program but I have to press enter key to get read the value into the variable.
#include
main()
{
char c;
while(c!='n')
{
scanf("%c",&c);
fflush(stdin);
printf("ok");
}
}
Is there any solution for this? Why?

2006-08-09 00:02:36 · 3 answers · asked by prasoonkg 1 in Computers & Internet Programming & Design

3 answers

Use curses library. Its a wonderful library for exactly what you are looking for. Using this library you can handle keystrokes i.e. a char is trapped as soon as it is pressed, you dont have to wait for enter key press.

if curses.h is not availble in your machine, then re-run Linux setup, select custom libraries, there you will find ncurses library.

You get more info at:
http://www.gnu.org/software/ncurses/ncurses.html
http://www.unet.univie.ac.at/aix/aixprggd/genprogc/curses.htm
http://www.gmonline.demon.co.uk/cscene/CS3/CS3-08.html -> great one to start off with

Good luck.

2006-08-09 00:20:13 · answer #1 · answered by Indian_Male 4 · 0 0

use the getch() function if you want to read a single char and not have it written to the console. If you want to read a char and have it written to the console then use getchar(). Neither function have any arguments to deal with and they only return characters from the keyboard, if either function returns zero it means that multiple keys have been pressed like Alt, if it dose return zero then you have to call the function again to find out what non-graphical keys were pressed. getch and getchar may not appear in your library of functions but it is in mine in the conio header.

2006-08-09 01:36:47 · answer #2 · answered by Anonymous · 0 0

Have you tried kbhit();

2006-08-09 00:56:30 · answer #3 · answered by ? 4 · 0 0

fedest.com, questions and answers