PDA

View Full Version : Xcode and getch()




kabuki4774
Jan 30, 2012, 02:35 PM
So I was looking to use the function getch() in a program of mine, and I'm not sure of how to get it to work properly.

Also I am choosing getch and not getchar or getche because I don't wish to have the users input displayed on the screen.

Any suggestions or advice will be greatly appreciated!



robvas
Jan 30, 2012, 03:05 PM
Isn't getch() and old Borland conio thing?

ScoobyMcDoo
Jan 30, 2012, 03:08 PM
Isn't getch() and old Borland conio thing?

It's also in curses.

So, curses is used for terminal i/O - you said you don't want users to input from the screen. What is your input medium? A file?

--- Edit

Nevermind - just re-read your post. I think you are saying you don't want an echo.

Did you read the getch man page? Are there any specific problems you are having?

robvas
Jan 30, 2012, 03:37 PM
I think if he was trying to use curses, he would know about the other stuff he'd have to do to get it to work right.

http://forums.macrumors.com/showthread.php?t=418230

What are you trying to do? Capture user input? Pause a program until someone hits a key? Read the arrow keys for a game?

kabuki4774
Jan 30, 2012, 05:21 PM
my goal is simply to have the program ask the user to make a selection from a menu. (the options are only 1-4)
the user then inputs his selection
after a selection is made it will appear on a new line stating a few brief words and his selection.

ScoobyMcDoo
your right. I don't want the echo. and thats the only problem I'm really having

I have not read the getch man page, where can I find that?

lloyddean
Jan 30, 2012, 05:37 PM
Run "Terminal.app" located at "/Applications/Terminal" then type into the terminals window -

man getch

followed by a return.

kabuki4774
Jan 30, 2012, 07:51 PM
thanks lloydean.

although the program still does work. I made some changes that the man getch told me to and now it wont build. so ill put up the code and see if you guys could see where I am going wrong.


#include <stdio.h>
#include <curses.h> //this is directive is in replace of conio.h

int getch(void); //i believe the man getch told me to declare getch as such

main () {

char *m,*b,*w,*l,*d,*a,*p;
int option;

m="\t\t Main Menu \t\t";
b="------------------------";
w="1- Word Perfect.";
l="2- Lotus 1-2-3.";
d="3- dBase IV.";
a="4- AutoCAD.";
p="Press the required number:";

printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s ",m,b,w,l,d,a,b,p);
option=getch(); //this is where the getch is used
printf("Your choice is: %c\n",option);


}

subsonix
Jan 30, 2012, 08:27 PM
You can not use getch() with the stdio.h functions, it will do nothing. You will need to re-write what you have with curses functions.

Compile with -lncurses btw.

ScoobyMcDoo
Jan 30, 2012, 09:13 PM
For this simple program you probably don't want to mess with curses. Instead just use getchar(), but change the termios stuff to do what you want:


#include <stdio.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>

main () {

char *m,*b,*w,*l,*d,*a,*p;
int option;
struct termios oldt, newt;

/* tcgetattr gets the parameters of the current terminal
* STDIN_FILENO will tell tcgetattr that it should write the settings
* of stdin to oldt
*/
tcgetattr( STDIN_FILENO, &oldt);
/*now the settings will be copied*/
memcpy((void *)&newt, (void *)&oldt, sizeof(struct termios));

newt.c_lflag &= ~(ICANON); // Reset ICANON so enter after char is not needed
newt.c_lflag &= ~(ECHO); // Turn echo off

/*
* Those new settings will be set to STDIN
* TCSANOW tells tcsetattr to change attributes immediately.
*/
tcsetattr( STDIN_FILENO, TCSANOW, &newt);

m="\t\t Main Menu \t\t";
b="------------------------";
w="1- Word Perfect.";
l="2- Lotus 1-2-3.";
d="3- dBase IV.";
a="4- AutoCAD.";
p="Press the required number:";

printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s ",m,b,w,l,d,a,b,p);
option=getchar(); //this is where the getch is used
printf("\nYour choice is: %c\n",option);

/*
* Restore original settings
*/
tcsetattr( STDIN_FILENO, TCSANOW, &oldt);
}
~