This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C/C++: Capture characters from standard input without waiting for enter to be pressed
I'm using C-Free 4 Standard on windows 7 and I writing a C program.
I'm using getch() as a function to pause the program, however, the character(s) pressed echos on the screen and it waits for the Enter key to be pressed before moving on (it doesn't look any different than how the scanf works). I tried getche(), and it works fine, although the echo appears.
What could be the problem with the getch() function?
The getch, wgetch, mvgetch and mvwgetch, routines read a character
from the window. In no-delay mode, if no input is waiting, the value
ERR is returned. In delay mode, the program waits until the system
passes text through to the program. Depending on the setting of
cbreak, this is after one character (cbreak mode), or after the first
newline (nocbreak mode). In half-delay mode, the program waits until a
character is typed or the specified timeout has been reached.
More or less the same method is used in Windows. You can use _getch() to get the input character available for the application without buffering.
In fact there are several ways to pause the execution of your program until you enter something, one way to do that is by using getchar() (which is part of the function set of stdio.h and an official standard library), It will do the same effect as getch() (this function is part of the functions set of conio.h library which isn't an official library).
If your problem is that you want to avoid pressing Enter every time you enter a character which is not very clear in your question), then read this: How to avoid press enter with any getchar()
Related
I am just playing around with ncurses and so, and I discovered a really weird behaviour.
When i use initscr() from the ncurses lib, and afterwards i use a normal getchar(), then the program terminates after pressing the first key.
The normal getchar() behaviour I would expect, is that i can type (more or less) as long till I press return.
#include "curses.h"
int main()
{
initscr();
getchar();
//endwin();
return 0;
}
Can anyone explain me why this happens?
curses initializes the terminal input to raw mode (which in turn makes the connected stream act as if it is unbuffered), so that curses can detect single-character input. getchar assumes that the input is buffered, so that you would press Enter to end an input line. In raw mode, getchar returns right away, because a read call will find something as soon as you press a key.
Besides endwin, you could use other curses functions for switching back/forth between the terminal's raw/cooked modes (see the manual for reset_shell_mode and reset_prog_mode).
This question already has answers here:
Capture characters from standard input without waiting for enter to be pressed
(21 answers)
Closed 6 years ago.
I'm trying an example from "The C programming language" on Windows with CodeBlock and I don't know how I can get user input without having to press enter. This program counts the number of lines in the input using getchar() and so when I run it "enter" only works as a \n and doesn't allow me to send my input. Is there an alternative way to do it?
EDIT
the problem was with the compiler. Figured it out after looking into it for some time
This is not a problem of the C programming language. It has more to do with the terminal that you are using to interface with the C program.
By default, most terminals buffer the input that you type and only send it to the program when you finish the line by pressing enter. This is what allows you to use the backspace key to correct typing mistakes before they are sent to the C program.
If you are not creating an interactive program, then if you redirect the program output to read from a file instead of from the terminal then getchar() will not have to wait. After all, the input file is all there from the start.
./myprogram.exe < myinputfile.txt
If you want to create an interactive program then the simplest thing you can do is to accept that the input comes one full line at a time. It is what users typing at the terminal will expect.
If you want your application to immediately respond to keypresses then you will need to use something outside the standard C library. For example, in Linux you could use ncurses to create terminal applications that can immediately react to a keypress, including arrow keys and so on.
I have developed a simple console utility in C which parses various text files.
IDE - Code Blocks
OS - windows
I intend to distribute its executable.
The executable works fine, however unlike when executed from the IDE, the execution does not pause/wait for keystroke at the end of execution.
I tried using getchar()/system("pause"), but the execution doesn't pause there.
Is there an alternative to wait for keystroke before ending execution, so that the user can view the output?
You can use
getchar();
twice , because its very likely that last '\n' newline character will get consumed by your getchar().
or use
scanf(" %c");
with that extra space
at the end of your file .
It depends on how other parts of your code receives input from the user (i.e. reading from stdin).
The getchar() approach will work fine if your program is not reading anything from the user, or is reading using getchar().
A general guideline, however, is to be consistent in style of input from every stream. Style of input refers to character-oriented (functions like getchar()), line-oriented (like fgets()), formatted (functions like scanf()), or unformatted (like fread()). Each one of those functions does different things depending on input - for example getchar() will read a newline as an integral value, fgets() will leave a newline on the end of the string read if the buffer is long enough, scanf() will often stop when it encounters a newline but leave the newline in the stream to be read next.
The net effect is that different styles of input will interact, and can produce strange effects (e.g. data being ignored, not waiting for input as you are seeing).
For example, if you are using scanf(), you should probably also use scanf() to make your program wait at the end. Not getchar() - because, in practice, there may well be a newline waiting to be read, so getchar() will return immediately, and your program will not pause before terminating.
There are exceptions to the above (e.g. depending on what format string is used, and what the user inputs). But as a rule of thumb: be consistent in the manner you are reading from stdin, and the user will have to work pretty hard to stop your program pausing before terminating.
An easier alternative, of course, is to run the program from the command line (e.g. the CMD.EXE command shell). Then the shell will take over when your program terminates, the program output will be visible to the user, so your program does not need to pause.
Don't use system("pause") since it's not portable. getchar should work on the other hand. Can you post some code? Maybe there's something on the keyboard buffer that's being consumed by your one and only getchar call.
I am writing a program which has a main menu, with a couple functions which I am allowing the user to access by typing in a number for one menu selections. For that, I am using _getch(). I need to be able to exit the program when the user enters the EOF key, but _getch() does not actually catch the EOF keystroke. I know I can get it to work by using getchar() instead of _getch(), but I would prefer not to have the user be forced to press enter after every function selection.
Is there a way to get _getch() to catch EOF? This is for Windows if it is of any importance, any help is really appreciated, as I have been unable to find anything out there.
The standard C input stream stdio is line buffered, and there is no defined way to forcibly flush it. If you want to avoid this and read from the buffer as every new character is entered, you have to use some OS specific magic, which is what _getch from conio.h tries to do.
There are other implementations out there of getch-type functions, for instance getch from the PDCurses library.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C/C++: Capture characters from standard input without waiting for enter to be pressed
How do you do non-blocking console I/O on Linux in C?
I am attempting to write a program in the C language which is in a constant loop but I need to be able to receive input from the keyboard at the start of each cycle or to continue on if there is no key being pressed.
getchar() and _getch() are insufficient as they both wait for the input. If anybody knows of a function similar to _getch but which doesn't wait for the input it would be much appreciated.
There's no C-standard way to do what you want to do (capture keystrokes from the keyboard without the user hitting enter), you have to go platform specific. <conio.h> should have what you need on Windows, you'd need curses on Linux to get this. (I suspect curses would work on Mac as well, but don't quote me on that)
On Windows kbhit() doesn't wait for any input, it just returns if there is a key pressed at the very instant you make the test. If you need to know what the key was as well, then you can combine this with getch()
something like:
while(countdown++ <= 1000){ // give a second to hit something
if(b=kbhit()) // if the user presses a key
break; // leave the loop early
Sleep(1); // else, sleep for 1 ms and try again
}
if (b != 0) // if something was pressed
printf("The key was %d\n", getch());