Mac Xcode and getch()

kabuki4774

macrumors newbie
Original poster
Jan 30, 2012
3
0
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!
 

ScoobyMcDoo

macrumors 65816
Nov 26, 2007
1,188
35
Austin, TX
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?
 

kabuki4774

macrumors newbie
Original poster
Jan 30, 2012
3
0
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?
 

kabuki4774

macrumors newbie
Original poster
Jan 30, 2012
3
0
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

macrumors 68040
Feb 2, 2008
3,551
79
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

macrumors 65816
Nov 26, 2007
1,188
35
Austin, TX
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:

Code:
#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);
}
~
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.