Welcome to Dream.In.Code
Become a C++ Expert!

Join 136,930 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,834 people online right now. Registration is fast and FREE... Join Now!




Linux C Programming Help

 
Reply to this topicStart new topic

Linux C Programming Help

S0n1C
1 Apr, 2008 - 11:21 PM
Post #1

New D.I.C Head
*

Joined: 15 Jun, 2007
Posts: 35


My Contributions
Hey everyone;

So a long time ago i created a little menu in C which allowed me to scroll through my options instead of typing in a character each time. The downfall to my menu was that I created it in windows, using the conio.h library. To my knowledge Linux doesn't have access to this library. The following is kinda what my code did.

CODE
char choice

choice = getch();


if (choice == 'ASCII Character')
{
    move option down
}


what choice = getch() allows me to do is press a key, and without pressing enter, something happened. Is there a similar way to do this on a linux platform, or should i get conio.h? Is there another command that also does the same thing?

Any suggestions would be greatly appreciated.

Thanks in advance;
S0n1C!
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: Linux C Programming Help
2 Apr, 2008 - 12:19 AM
Post #2

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 6,495



Thanked: 66 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
You can't use conio.h for Linux consoles.

You'll have to use tput & other screen functions, & just use logic to determine which line the user is currently accessing. At least that's how I've done it.
User is online!Profile CardPM
+Quote Post

jpw1991
RE: Linux C Programming Help
2 Apr, 2008 - 02:33 AM
Post #3

New D.I.C Head
*

Joined: 13 Aug, 2007
Posts: 33


My Contributions
There's a getch() in ncurses.h. Here's a link. Or you can just create your own getch function:
CODE
#include <termios.h>
#include <unistd.h>

int getch() {
    struct termios oldt, newt;
    int ch;
    tcgetattr(STDIN_FILENO,&oldt);
    newt = oldt;
    newt.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO,TCSANOW,&newt);
    ch = getchar();

    tcsetattr(STDIN_FILENO,TCSANOW,&oldt);
    return ch;
}
Best to use the one in ncurses.h though I think.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 10:35PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month