Become a MacRumors Supporter for $25/year with no ads, private forums, and more!

tombrownington

macrumors newbie
Original poster
Jan 19, 2008
7
0
hi,
i am new to the mac, and just started using xcode. i recently learned that the conio.h - i used its getch function regularly - header file doesn't exist on unix platforms. searching for an alternative i found that the curses.h header file provides a similar function.
i tried to write and compile this basic program in xcode but it gives me errors.

#include <iostream>
#include <curses.h>
using namespace std;

int main () {
cout << "Hello, World!" <<endl;
getch();
return 0;
}

xcode gives me 2 building errors:
1) "_stdscr", referenced from:
_stdscr$non_lazy_ptr in main.o
2) "_wgetch", referenced from:
_main in main.o
symbol(s) not found
collect2: Id returned 1 exit status

what should i do to get this program working (i'd rather compile directly from xcode rather than from console - i'm not too familiar with the latter). can anyone point me in the right direction?

P.S. I'm running xcode 3 on an intel iMac with leopard installed.
 

ScoobyMcDoo

macrumors 65816
Nov 26, 2007
1,188
35
Austin, TX
You don't wanna use curses. Basically, you have to turn off line buffering to do a single character read. Here's an example:

#include <iostream>
#include <termios.h>

int mygetch( );

int main (int argc, char * const argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
char ch;
ch = mygetch( );
std::cout << "You hit the " << ch << " key" << std::endl;
return 0;
}

int mygetch( ) {
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;
}
 
  • Like
Reactions: imbilalbutt

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
If you are running in Xcode, what need do you have of getch() anyway? You get a Run Log window that does not go bye-bye like the DOS box (ergo - command prompt window) does.

Todd
 

tombrownington

macrumors newbie
Original poster
Jan 19, 2008
7
0
Or, use getchar().

I already did, and it works fine. What you suggest is probably the best alternative.
Still out of curiosity I'd like to know why my original code doesn't compile. Anybody help me with that?
Thanks for the help anyway, toddburch.
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
You original code, with the getch() turned into getchar(), and the curses.h include removed, works fine for me.

Code:
#include <iostream>
using namespace std;

int main () {
cout << "Hello, World!" <<endl;
getchar();
return 0;
}

Todd
 

fimac

macrumors member
Jan 18, 2006
95
0
Finland
Still out of curiosity I'd like to know why my original code doesn't compile. Anybody help me with that?

You need to add the curses library to the link command. (The error messages you get are simply the linker telling that it does not have enough information to finish linking.) The easiest way is to add "-lcurses" to your target's "Other Linker Flags".

(Note: Curses requires initialization and shutdown calls, see "man curses" or search XCode help. You should not use printf/cout, instead curses provides printw et al.)

Code:
#include <iostream>
#include <curses.h>
using namespace std;

int main(void)
{
  initscr();
  cbreak();
  noecho();
  printw("Hello, world!");
  getch();
  endwin();
  return 0;
}
 

rdettogni

macrumors newbie
Nov 15, 2010
10
0
Thanks!!

I was having a similar problem and removing curses.h and using getchar solved everything!!!! Thanks!!!
 

7b7hdz

macrumors newbie
Jun 21, 2017
1
0
Use the cin.get() function for example:

#include <iostream>

using namespace std;

int main()
{
char input = cin.get();

cout << "You Pressed: " << input;
}

//The program would then wait for you to press a key
//Once you have, the key you pressed would be printed to the screen
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.