LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   getchar() problem (also conio.h) (https://www.linuxquestions.org/questions/programming-9/getchar-problem-also-conio-h-666995/)

faizlo 09-02-2008 12:09 AM

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
//09/01/08
//
#include <iostream>
//#include <ncurses.h>
using namespace std;

int main()
{
 char dir = 'a';
 int x = 10, y = 10;

 while (dir != '\r')
    {
      cout << "\nYour coordinates are: \n" << x << ", " << y << ".\n";
      cout << "Enter the direction you like: (n,s,e,w)\n";
      dir = getchar();
      switch(dir)
          {
            case 'n': y++; break;
            case 's': y--; break;
            case 'e': x++; break;
            case 'w': x--; break;
            case '\r': cout << "\nExiting the program, bye bye!\n"; break;
            default: cout << "\nYou made a mistake! Try again\n";
          }    //End of switch
    }          //End of while
 return 0;
}              //End of main()

As you see, I am trying to make the executable exit if the user hits the "return" key. But the code in its current shape does not do that.

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

Guttorm 09-02-2008 06:16 AM

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:

...
 case '\n':
 case '\r': cout << "\nExiting the program, bye bye!\n"; break;
 ...


ta0kira 09-02-2008 08:30 AM

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

chrism01 09-02-2008 06:23 PM

iirc, conio.h is MS (DOS) only. To Unix, all 'consoles' are virtual, so you have to do it differently.

faizlo 09-03-2008 04:37 PM

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

ta0kira 09-03-2008 05:08 PM

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

dmail 09-03-2008 06:36 PM

Quote:

Does that mean you're programming in Windows?
For this post I am assuming so.

Quote:

Originally Posted by faizlo (Post 3268753)
Hi,

Thank you all for the help,

Guttorm: Your suggestions did not solve the problem!

This may sound like a silly question but you did change the expression in the while test did you not? If you did not then the loop would still not exit as you just break out of the switch loop and the test is true. You either need to change the while test to be platform specific and check != '\n' or return from the main function when the correct button is detected.
Code:

...
case '\n' : return 0;
...


faizlo 09-03-2008 10:17 PM

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

graemef 09-03-2008 10:41 PM

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

faizlo 09-04-2008 04:16 AM

Hi,

Here is an edited version of the code:
Code:

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

int main()
{
 char dir = 'a';
 int x = 10, y = 10;

 while (dir != '\r')
    {
      cout << "\nYour coordinates are: \n" << x << ", " << y << ".\n";
      cout << "Enter the direction you like: (n,s,e,w)\n";
      dir = getch();
      switch(dir)
          {
            case 'n': y++; break;
            case 's': y--; break;
            case 'e': x++; break;
            case 'w': x--; break;
            case '\r': cout << "\nExiting the program, bye bye!\n"; return 0;
            default: cout << "\nYou made a mistake! Try again\n";
          }    //End of switch
    }          //End of while
 return 0;
}              //End of main()

Now, it seems I have something with the "while" statement, as the code runs forever with the first two cout statements in the while function.

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

graemef 09-04-2008 06:09 AM

try breaking on a character other then enter (maybe 'q')

faizlo 09-04-2008 03:34 PM

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

graemef 09-04-2008 05:02 PM

you need to initialise the curses program, and also clean up at the end. Try:
Code:

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

int main()
{
 initscr(); // initialise ncurses
 cbreak();
 char dir = 'a';
 int x = 10, y = 10;

 while (dir != 'q')
    {
      cout << "\nYour coordinates are: \n" << x << ", " << y << ".\n";
      cout << "Enter the direction you like: (n,s,e,w)\n";
      dir = getch();
      switch(dir)
          {
            case 'n': y++; break;
            case 's': y--; break;
            case 'e': x++; break;
            case 'w': x--; break;
            case 'q': cout << "\nExiting the program, bye bye!\n"; break;
            default: cout << "\nYou made a mistake! Try again\n";
          }    //End of switch
    }          //End of while
 endwin(); // clean up routine
 return 0;
}

Check out man ncurses.

wget 09-04-2008 05:12 PM

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>

int main()
{
 int dir=0;

 int x = 10, y = 10;
 initscr();
 noecho();
 cbreak();

 printw("Your coordinates are: %d,%d\n", x, y);

 printw("Enter the direction you like: (n,s,e,w)\n");
 
  while( (dir = getch()) != '\n'){
   
      clear();

      switch(dir)
          {
            case 'n': y++; break;
            case 's': y--; break;
            case 'e': x++; break;
            case 'w': x--; break;
         
            default:{
                        printw("You made a mistake! Try again\n");
                        getch();
                        clear();
                        break;
                }
          }    //End of switch

        printw("Your coordinates are: %d,%d\n", x, y);

        printw("Enter the direction you like: (n,s,e,w)\n");
       
    };        //End of whilei
       
    endwin();

 return 0;
}              //End of main()


faizlo 09-05-2008 12:30 AM

Graemef: Here is the output of your code:
Code:

You made a mistake! Try again

                            Your coordinates are:
                                                  10, 10.
                                                          Enter the direction you like: (n,s,e,w)

            You made a mistake! Try again

                                        Your coordinates are:
                                                              10, 10.
                                                                      Enter the direction you like: (n,s,e,w)
                        w
                        Your coordinates are:
                                              9, 10.
                                                    Enter the direction you like: (n,s,e,w)
      e
        Your coordinates are:
                              10, 10.
                                    Enter the direction you like: (n,s,e,w)
                                                                            e
                                                                            Your coordinates are:
              11, 10.
                    Enter the direction you like: (n,s,e,w)
                                                            e

This is exactly how does it appear on my screen.

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.