getchar() problem (also conio.h)
Hi, I am glad to be here, I like this forum and this is my first contribution (well, if you consider a question a contribution!)
I have been trying to learn C++ recently using R. Lafore's book (OO programming with C++) I am trying to make this code work: Code:
//Switch with adventure In the book the author uses "conio.h" and I tried to use both "curses.h" and "ncurses.h" - you see I commented the later as the code worked without it. So, what should I do if I want the code to exit if the user hits "return" or "enter"? Thank you, faizlo |
Hi
The example has a case for the '\r' character, but writes newlines with the '\n' character. Windows has always been strange at handling this. Try this: Code:
... |
You also need to store the return of getchar in an int and not in a char because an error or EOF return will be outside of the range of the char type.
ta0kira |
iirc, conio.h is MS (DOS) only. To Unix, all 'consoles' are virtual, so you have to do it differently.
|
Hi,
Thank you all for the help, Guttorm: Your suggestions did not solve the problem! Ta0kira: I changed the variable dir from char to int and it is still the same, the code still do not exit when I hit return. chrism01: I know conio.h is a Microsoft nonsense, so my question is still as it is on the first post, how to edit the code so that it will exit when the user his the return key? faizlo |
Does that mean you're programming in Windows? Normally to even use getchar before 'enter' is hit you have to disable canonical mode. What were you doing with curses? That should handle the terminal modes, otherwise you'll have to do it manually. I can tell you how to do that in Linux, but not in Windows.
ta0kira |
Quote:
Quote:
Code:
... |
Hi again,
I use Linux only. In fact I have not touched an MS Windows box for 8 years now! dmail: I did not change anything in the while test expression, I only added the two lines mentioned in Guttorm post. What should I change? please tell me more. faizlo |
As I understand it getchar() will store the details in an internal buffer and only respond once a) the buffer is full or b) return is entered. So the program that you are attempting will not work as it is.
I believe to get instant response from the keyboard in a console application you will need to use something like ncurses |
Hi,
Here is an edited version of the code: Code:
// So, I guess I need a closer look at the while function. Any help would be appreciated. You need to compile this with -lncurses. faizlo |
try breaking on a character other then enter (maybe 'q')
|
I did and the loop rolls over and over. What exactly is going on in this code?
Can any one help me by trying this code? thank you, faizlo |
you need to initialise the curses program, and also clean up at the end. Try:
Code:
#include <iostream> |
Following on
And following on from the post above, an quick example, although ehm, there's not much C++ in there. Having you considered using cin ?
Code:
#include <curses.h> |
Graemef: Here is the output of your code:
Code:
You made a mistake! Try again wget: your code worked, but it changes the coordinates directly on the first line. Yes, it quits with a return his and gives an error if none of the (n,s,e,w) characters are hit. Yet I still want to know the following: 1- Is it that difficult to find a substitute for this "non ISO/ANSI" file "conio.h"? 2- How this code should be modified so I can get the results and messages I want? thank you all so much for your time and help, faizlo |
All times are GMT -5. The time now is 06:58 AM. |