0

I wrote a code on windows, using the function getch() from stdio. The thing is I have to use an input function that does not require pressing enter.

My code compiles and works perfectly on windows. However, this assignment has to run on linux, and when I try to do so, it tells me it does not recognize getch() (or _getch()). The problem is that according to the assignment, I can not use other includes but stdio.h (and same goes for adding a flag), so I can not use curses.h to solve this.

I also can not use termios.h and so on, we have not learned it. How can I solve this? Are there any other options?

Thanks

Francesco Montesano
  • 8,485
  • 2
  • 40
  • 64
Mike
  • 195
  • 2
  • 10
  • The `getchar()` function can retrieve a single character, but it won't (by default) receive an answer until the carriage return is pressed. You must disable buffering in order to get past that limitation however your question suggests you're not permitted to do that -- meaning you cannot complete this assignment. – mah Nov 16 '13 at 20:39
  • Seems like the answer is 'can't be done' but if that's the assignment the prof probably had something in mind. Maybe paste the instructions so someone can make sense of them for you. – Charlie Burns Nov 16 '13 at 21:09
  • I have completed a big part of the assignment, while using the getch function and running on windows, I only have the problem with linux. The instrucions say "Get a base from user, (base can be int in the range 2-36, when a=10, b=11,..., z=35, similarly to hex). Afterwards get a number in this base, without enter between any two digits (example for input - 1g5 in base>=17)." – Mike Nov 16 '13 at 21:19
  • The way I read that is one input line would be "17 1g5" and another input line "16 ff" etc etc. With an enter between each line. I can't believe they want you to get unbuffered input. So, read each line, get the base and then get the number. Then get another line ad do it again until there are no more lines. – Charlie Burns Nov 17 '13 at 01:53
  • the input rules are such that in one line i get the base (number from 2 to 36, no problems here), and here comes enter, so i just use scanf for this. the problem is afterward, i have got the base and enter, now i have to get an unknown number of chars, with no enter between them. here is an example for the program run: " 28 /new line/ 3a2 /new line/ 2634 " frst line is base, second number in base, third the number in base 10. – Mike Nov 17 '13 at 05:44

2 Answers2

2

The library approach on UNIXes is to us ncurses but it is a bit of a framework which isn't entirely trivial to set up. The non-library mode is to turn the standard input stream into non-canonical input mode using tcgetattr() and tcsetattr() with the file descriptor 0. Typing the necessary code from memory (i.e., I can't test it now and I probably forgot something important) the corresponding code looks something like this:

struct termios setings;
tcgetattr(0, &settings);
settings.c_lflags &= ~ICANON;
tcsetattr(0, &settings);

Clearly, a real implementation would verify that the system calls are actually successful. Note that after this code std::cin and stdin will immediately react on key presses. As a direct consequence, all kind of "funny" characters will be passed through. For example, when the delete key is used you'll see backspace characters (ctrl-H, char(7)) show up.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • I wish I could understand a word you have wrote. I am only on my first curse, and we have not learned 'termios'. Morever, I can not use it, as I said. – Mike Nov 16 '13 at 21:00
  • Well, if you can't use `` (or a library wrapper around that like ncurses) you won't get a setup which doesn't wait for the enter key! Well, I think you can play tricks on the shell or reading from a pseudo-tty but these will be more tricky than using `tcgetattr()` and `tcsetattr()`. – Dietmar Kühl Nov 16 '13 at 21:07
  • 1
    BTW, I _don't_ buy the "I haven't learned that" argument _at all_! Modulo potential minor errors which be can resolved with a simple Google search (if there are any errors) I just thought how to use these functions! ... and I can guarantee you that a lot of the things I tell others over here I was never "taught" but found them out from whatever source be it man pages, books, etc. – Dietmar Kühl Nov 16 '13 at 21:17
  • Well, we have not learned 'struct', neither 'pointers', and the way you wrote the code is not familliar to me, so I can not really understand what is going on there. – Mike Nov 16 '13 at 21:26
1

We don't do miracles in Linux. You either use other things besides stdio.h, or go without any equivalent of getch and do depend on Enter being pressed.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243