Results 1 to 9 of 9
hey..
so i have this program that's supposed to printout a line whenever i type 'n'..i cant use scanf() function coz whenever i click it, it wont read the 'n' ...
- 10-14-2007 #1Just Joined!
- Join Date
- Oct 2007
- Posts
- 60
using scanf() without hitting return
hey..
so i have this program that's supposed to printout a line whenever i type 'n'..i cant use scanf() function coz whenever i click it, it wont read the 'n' except until i press return (Enter)...but this is not what i want.iwant it to display that line exactly after i press 'n', sort of like when u use the up and down arrows when u use the command: "less file.c"
any help?
- 10-14-2007 #2
tcstattr() is your friend.
Start by doing this at the command line:
Then look at, compile, modify, and otherwise play with these tiny complete C programs which use tcsetattr().Code:man tcsetattr
I had to upload them with extension .txt, because this site wouldn't let me upload them as .c. So you'll have to remove that before playing with them.
Hope this helps.
- 10-15-2007 #3
I just use getch(). Might not be too portable though.
- 10-15-2007 #4
- 10-15-2007 #5
Actually, as part of the curses library, getch() is quite portable.
So you'd be using the curses library. Although the calls are simpler than the programs I've included here, there can be complications if you don't wish all the other screen structuring capabilities that curses gives you.
For more information, google this:
Code:curses tutorial
- 10-15-2007 #6
Well, here's a useful example of a curses menu:
getch does what's called "Unbuffered Input" meaning that every character typed is read as it is input. It's extremely useful, like when you do "Press any key to continue". Notice that getch returns an int that is actually a char. The reason I say it is not portable though, is because AFAIK it is included in curses. Of course, it will work on most unix boxes, but probably not on Windows. wje's hints, albeit somewhat tedious methods, allude to the 'right' way to do it.Code:void menu(Person *list, char *fileName) { int key, menuSelection = 0; initscr(); while (1) { draw(menuSelection); keypad(stdscr, TRUE); noecho(); do { key = getch(); switch(key) { case KEY_DOWN: menuSelection++; if (menuSelection > MENUITEMS - 1) menuSelection = 0; break; case KEY_UP: menuSelection--; if (menuSelection < 0) menuSelection = MENUITEMS - 1; break; default: break; } draw(menuSelection); } while (key != '\n'); }
- 10-15-2007 #7
- 10-15-2007 #8
portability to Microsoft(spit)(R) Windows
Neither tcgetattr() nor the curses library is available for Windows, as far as I know.
- 10-15-2007 #9
O I thought tcgetattr was part of the C standard, but it looks like it's in unistd.h, so that's a no for win compatibility. IDK, some people program on Windows, not me lol.


Reply With Quote
