I have a very simple code:
char character;
std::cin >> character;
However, I would like it to behave as follow:
Don't echo to console the character I type
std::cin should return (unblock) right away when a character is pressed without having to press the Enter key
Is this possible? I also have access to Qt.
This is going to be platform dependent, but you can use
getch() which is part of conio.h if you're on windows
or
getch() as part of curses.h if you're on *nix
References
http://opengroup.org/onlinepubs/007908799/xcurses/curses.h.html
http://en.wikipedia.org/wiki/Conio.h
Related
I have just started learning c++ and I want the programme to stay open after the result is displayed. So I used getch(); and c++ is showing that it should have a prototype. What does it mean? and how do I resolve this>
it means one of the followings:
you are programming under DOS and forgot to include conio.h (https://en.wikipedia.org/wiki/Conio.h). Possibly you copied a source from an older textbook, since conio.h is a very old concept. What sources do you use for learning? I'd recommend one from: The Definitive C++ Book Guide and List
You are programming under Linux and you forgot to include curses.h (http://linux.die.net/man/3/getch)
Include:
1) <conio.h> on Windows.
2) <curses.h> on UNIX
Looks like you only want to PAUSE your console application on the screen. Use this#include <stdio.h> and try getchar(); instead of getch(); or simply system("pause"); or cin.ignore() will do the job for you.
Also, "Start without Debugging" using Ctrl-F5 will allow you to Press Any Key To Continue at the end of your program. This way it won't close until you press some key and the console will pause on the display screen.
If you're on Windows then getch is a function from the Windows-only <conio.h> library. You need to include it (#include <conio.h>).
It's possible only on Windows.
Also, getch() is deprecated.
Use _getch() instead.
If you're on GNU+Linux, then getch is a function from the <curses.h> library. You need to include it (#include <curses.h>).
The objective as I understand is :
I want programme to stay open after the result is displayed
Why not do it the typical c++ way?
#include<iostream>
int main(void)
{
int i;
char ch;
std::cout<<"Enter any character : ";
std::cin.get(ch); // For testing enter a string at this step say "String"
/* The input to cin is line-buffered, so after reading 'S' to ch,
* the remaining "tring" is still in the buffer.
*/
std::cout<<"Entered character : "<<ch<<"\n";
while(std::cin.get()!='\n')
;;
/* cin.get() is an overloaded function in the istream class.
* If no arguments are passed to 'get()' this function reads single next character
* In essence, we wait for the cin.get() to clear the buffer that is
* read all characters including '\n'
*/
std::cout<<"Press any key to continue..\n";
std::cin.get();
/* Since we have already cleared the buffer using the loop
* 'get()' expects us to enter a character this time
*/
return 0;
}
I am new to c++ ,
What code should i write to make the screen stand still. I use
getche();
in c language. but instead of getche() what should i use in c++;
I tried
std::cin.get();
but the console windows displays and then goes off quickly.
the console windows displays and then goes off quickly.
It seems that you have something in your buffer, and cin.get is reading that as a character. For example:
int i = 0;
std::cin >> i;
std::cin.get();
When you enter number and press Enter, cin >> i will consume the number, but it will leave \n character (which comes from Enter keystroke) in the buffer which will be read by std::cin.get() without waiting for the user to enter new data.
In order to make this example to behave like we want, we need to empty the buffer before calling std::cin.get():
int i = 0;
std::cin >> i;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
std::numeric_limits is defined in <limits> header file.
In Windows, I usually uses system("pause");, which call the OS's pause command, to prevent the console window from being closed after the program terminates. This commands displays a nice message and wait for any key :).
Press any key to continue . . .
I don't know if the command exists in the other OSes or not, nor I don't know if it is what you're trying to archive :).
The function is in the cstdlib or stdlib.h header file
Last but not least, it's not really a good idea to call getch() at the last line of code to prevent the console from being closed. But if you really prefer this way, I suggest to use something like
std::string temp;
std::cin>>temp;
At the ends of your main function, just before return statement.
Just include <stdlib.h> and in main method use system("pause") your console window will remain still.
Please use the following
getch()
When using the >> operator in c++ to capture user input, is it possible to prevent the console from printing the newline that is generated when the user presses the return key?
You cannot prevent newline character, because when you use cin, you are communicating with system core, which is not under control by users. console will return, when you enter \n or EOF or other exception situation.
So the better way is to use getchar() to capture the '\n', and do not leave it in buffer.
It is possible to prevent this newline behavior by inputting two EOFs instead of Carriage Return from the keyboard. After entering your string at the console prompt, hit
CTRL-D, CTRL-D
Note, this is a platform specific answer. This works on my Mac, but on Windows OS the EOF sequence may be CTRL-Z, RETURN. I would appreciate an answer edit <-- HERE.
Alternately, you can ditch the >> operator and use something like std::getline and specify an exact string termination delimiter. For example:
std::string myString;
std::getline(std::cin, myString, ';');
std::cout << myString;
This will read from standard input to myString, and put the string terminating NULL character where it finds the first semicolon ';'. Then you'll only have to hit CTRL-D (input EOF) once.
You can enter the values or input by pressing space every time. But at the end you must press enter key.
Let's say: you want to enter "5,4,3,2,1"
You can do: 5 [enter] 4 [enter] 3[enter] 2[enter] 1[enter]
Also: 5[space]4[space]3[space]2[space]1[enter]
But if you want to print the output near input, you can simply print the input first and than you can print the what you want.
Example:
Input: 3 Output: input+1
So you will do:
cout<<input;
cout<<" "<<input+1<<endl;
Good luck :)
When I'm using PDcurses and I try to have a while loop exit when the enter key is pressed with while(key != KEY_ENTER), the while loop never exits. However, when I try to have the same loop exit with while((char)key != '\n'), it exits successfully whenever I pressed enter. Why does '\n' work and not KEY_ENTER?
btw, key is an int
and I hope this is the relevant few lines of the code:
int key;
while((char)key != '\n') {
key = getch();
...
}
getch() is a function defined by the ANSI C standard for the C runtime library.
On most systems, such as Windows, Linux, etc., this function is implemented to return '\n' when the user pressed Enter. For Comparison, on Windows the key-press itself (of Enter) might be represented as the key-code VK_ENTER.
PDCurses is translating the key codes to ASCII values for you.
You can get the key values you want if you first call the PDCurses functions raw(); nonl();. Also, you should probably use wgetch() for new code.
KEY_ENTER == 0x157, '\n' == 0xA
'\n' is the standard ASCII newline, while KEY_ENTER represents a keyboard code. See the PDCurses code.
For more information, you should post the relevant part of your code.
This bit of code runs infinitely:
copy(istream_iterator<char>(cin), istream_iterator<char>(), back_inserter(buff));
The behavior I was expecting is that it will stop when I press enter.
However it doesn't.
buff is a vector of chars.
I assume you are typing stuff in at the keyboard.
The enter key doesn't signify the end of the stream. It's just another character from cin's perspective. You need to submit EOF to achieve this (Ctrl+Z, Enter on Windows and Ctrl+D on Unix/Mac).
Incidentally, this isn't the usual way to read characters from the console. It is very inefficient (istream_iterator calls operator>> for each character) and will misbehave with whitespace. To read a line of data entry, use getline instead.