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

5
  • 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. Commented Nov 16, 2013 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. Commented Nov 16, 2013 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)." Commented Nov 16, 2013 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. Commented Nov 17, 2013 at 1: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. Commented Nov 17, 2013 at 5:44

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
Well, if you can't use <termio.h> (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().
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.
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.
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.

3 Comments

I am aware of that, but the assignment input rules are such that require getting chars excatly like that.
Then your assignment is impossible. I'm really sorry.
@n.m.they could have included a standard C++ function for this in standard libraries right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.