Xcode and getch()

Discussion in 'Mac Programming' started by kabuki4774, Jan 30, 2012.

  1. kabuki4774
    Expand Collapse
    macrumors newbie

    Joined:
    Jan 30, 2012
    #1
    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!
     
  2. robvas
    Expand Collapse
    macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
  3. ScoobyMcDoo
    Expand Collapse
    macrumors 65816

    Joined:
    Nov 26, 2007
    Location:
    Austin, TX
    #3
    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?
     
  4. robvas
    Expand Collapse
    macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #4
    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?
     
  5. kabuki4774
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    Jan 30, 2012
    #5
    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?
     
  6. lloyddean
    Expand Collapse
    macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #6
    Run "Terminal.app" located at "/Applications/Terminal" then type into the terminals window -

    man getch

    followed by a return.
     
  7. kabuki4774
    Expand Collapse
    thread starter macrumors newbie

    Joined:
    Jan 30, 2012
    #7
    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);


    }
     
  8. subsonix
    Expand Collapse
    macrumors 68040

    Joined:
    Feb 2, 2008
    #8
    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.
     
  9. ScoobyMcDoo
    Expand Collapse
    macrumors 65816

    Joined:
    Nov 26, 2007
    Location:
    Austin, TX
    #9
    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);
    }
    ~                                                                                    
    
     

Share This Page