 |
 |
<curses.h>
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Nov 2002
Status:
Offline
|
|
Take a look at this code:
#include <stdio.h>
#include <curses.h>
int main()
{
char ch;
printf("Enter a letter: ");
ch= getch();
printf("That letter is %c.", ch);
}
When i try to compile this i get the error:
ld: Undefined symbols:
_stdscr
_wgetch
I want to be able to use the getch() function to get a single character (dont feel like flucking around with getchar()'s line buffer). Does anyone know how i can fix this.
|
|
|
| |
|
|
|
 |
|
 |
|
Forum Regular
Join Date: Oct 2000
Location: Portland, OR USA
Status:
Offline
|
|
I believe you need to compile with the curses library with '-lcurses' (or '-lncurses' as that's the implementation used now).
|
|
|
| |
|
|
|
 |
|
 |
|
Fresh-Faced Recruit
Join Date: Nov 2002
Status:
Offline
|
|
It compiled, but when i call the getch() (with 'ch = getch()') function it seems to skip it, and i cant enter a letter. It just displays the unknown symbol, and quits.
And why do i have to use the -l option with the curses header, but not stdio.h, math.h, etc.?
|
|
|
| |
|
|
|
 |
|
 |
|
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status:
Offline
|
|
Originally posted by Spiffster:
It compiled, but when i call the getch() (with 'ch = getch()') function it seems to skip it, and i cant enter a letter. It just displays the unknown symbol, and quits.
And why do i have to use the -l option with the curses header, but not stdio.h, math.h, etc.?
stdio.h and math.h are virtually part of the language. The stdio libs are automatically included. With some compilers, -lm is required for the math libs. ncurses is much less standard than the math libs. Virtually all libraries must be specifically listed when compiling. ncurses is not exempt from this.
|

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Jan 2001
Location: California, USA
Status:
Offline
|
|
Hi!
Did this ever get resolved?
I've been experiencing the same problem where the program just runs without pausing to let me enter anything. I've done some research and it seems as though I should call nodelay() and set it to FALSE. Sadly, I have no idea what to pass as the first argument for this function:
int nodelay(WINDOW *win, bool bf);
Obviously, it wants a pointer to a window object of some sort, but I have no idea what to pass in OS X for a command line tool. Any ideas? Thanks!
-Joel
P.S. I've set the '-lcurses' flag and everything compiles fine, it just doesn't do what it's supposed to.
P.P.S. Below is what I'm trying to run.
#include <iostream>
#include <stdio.h>
#include <curses.h>
using namespace std;
int main ()
{
char test;
cout << "Input: ";
test = getch();
cout << endl << "test= " << test << endl;
}
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
If you're not using any features of curses, why are you including the header and linking against it?
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Jan 2001
Location: California, USA
Status:
Offline
|
|
Originally posted by Angus_D:
If you're not using any features of curses, why are you including the header and linking against it?
It was my understanding that curses.h contains the definition/implementation for getch(). Am I just misinformed on this point?
-Joel
|
|
|
| |
|
|
|
 |
|
 |
|
Addicted to MacNN
Join Date: Mar 2000
Location: London, UK
Status:
Offline
|
|
Apparently so, but why not just use getc() instead?
|
|
|
| |
|
|
|
 |
|
 |
|
Junior Member
Join Date: Jan 2001
Location: California, USA
Status:
Offline
|
|
Originally posted by Angus_D:
Apparently so, but why not just use getc() instead?
Ok. I tried the following:
#include <iostream>
#include <stdio.h>
using namespace std;
int main ()
{
char test;
cout << "Input: ";
test = getc(stdin);
cout << endl << "test=" << test << endl;
}
That works, but defeats the reason why I would do it this way to begin with. I'm attempting to use this in a menu function to avoid the necessity for the user to press return after entering their selection. getc() in this case requires a return. If I have to do it that way, I would just use cin. As far as I've seen, getch() is the only way to accomplish this (hence, my desire to use the version in curses.h).
-Joel
|
|
|
| |
|
|
|
 |
|
 |
|
Senior User
Join Date: Dec 2002
Location: Portland, OR
Status:
Offline
|
|
I'm no programmer, but when I went through a C class in school recently I had the same problems you are having now. What I found out (via Google..) is that getch() is not portable and is not available on most Unixes. This is due to the stream based input/output system that Unix uses.
I think what you will have to do is write a function that will read the keyboard directly, or find a snippet somewhere that is already written. If you could find the name of it I wouldn't be a bit surprised if there is one in the GLIBC Gnu C libraries...
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Elite
Join Date: Feb 2000
Location: Nashua NH, USA
Status:
Offline
|
|
Looks like you'll need to create an event loop and poll for key strokes, don't know how its done in unix. Someon else may.
|
|
|
| |
|
|
|
 |
|
 |
|
Mac Enthusiast
Join Date: Jan 2001
Status:
Offline
|
|
Originally posted by Joel:
I'm attempting to use this in a menu function to avoid the necessity for the user to press return after entering their selection. getc() in this case requires a return.
check out the cbreak() function
% man cbreak
|
|
|
| |
|
|
|
 |
 |
|
 |
|
|
|
|
|

|
|
 |
Forum Rules
|
 |
 |
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
 |
 |
 |
 |
|
 |
|