What code should be used instead of getche in c++ - c++

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()

Related

C++: getline freezes at end of file

I want to read in one file line-by-line and output each line I read to a new file. In this code, cin has been redirected to refer to the input file, and cout has been redirected to refer to the output file.
The loop successfully writes every line in the file, but then it gets stuck on the final getline call. As a result, "Done" is not written to the file and the program does not terminate.
#include <string>
#include <iostream>
using namespace std;
int main() {
string line;
while(getline(cin, line)) {
cout << line << endl;
}
cout << "Done";
return 0;
}
Strangely, if I forcibly terminate the program, it seems to suddenly execute as desired, with "Done" being written.
Can someone point me in the right direction? Is there a flaw in the code, or is this some external configuration issue?
Notes: The input file in question ends with a newline character. Also, I do not want to use any includes besides these two.
The code should terminate on end of file (EOF) or any sort of file error. (The getline being called is:
http://en.cppreference.com/w/cpp/string/basic_string/getline
It returns the cin istream and then invokes its boolean conversion operator:
http://www.cplusplus.com/reference/ios/ios/operator_bool/
that checks if badbit or failbit is set on the stream. The failbit state should be set when a read is attempted with the stream already at EOF, or if there is an error.)
Per the comments above, it seems like this does work when the code is run from the shell directly. My guess is Eclipse is doing something complicated where it either intentionally sends the file into the program and then switches to an interactive input mode, or has a bug in which it doesn't close its end of a pipe or pty/tty it is using to send input to the program. (I.e. Eclipse is not binding stdin directly to the file itself in running the program.)
If one wanted to debug it further, one could look at the process state using tools like lsof. (Assuming a UNIXy system.) Might also be worth raising the issue in an Eclipse forum. The IDE is not my area of expertise.

getch() showing prototype error

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;
}

How can I tell the program to stop using freopen

I am beginner in C++ and I have a question that is beyond my limits.
I compile under GNU GCC.
I use
#include<stdio.h>
also known as:
#include<cstdio>
At some point in my program I tell the program to use the file de_facut.txt as an in file:
freopen("de_facut.txt","r",stdin);
How can I tell the program to use the console to put the input (as default) instead of the in file?
First I want to read from that file, but later in the program I want the user to enter input in the console.
I hope you understood my problem, I am not very good at english.
The same question has been asked about stdout: How to redirect the output back to the screen after freopen("out.txt", "a", stdout), but the answer is the same for both - there's no clean way of doing this: http://c-faq.com/stdio/undofreopen.html
Since you do need to read from the console later in the program, I'd suggest you just open the file as, well, a file. If the reason you wanted to use stdin to read from a file is the convenience of not having to pass the file handle to functions like fscanf, you could consider using fstream facilities instead - the code can look exactly as when reading from the console:
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
int x;
cin >> x; // reading from console
{
ifstream cin("input.txt");
cin >> x; // reading from file
}
cin >> x; // again from console
return 0;
}
Use fclose():
FILE *fp=freopen("data.txt","r",stdin);
/*your code here*/
fclose(fp);
ref: C library function - freopen()
In windows,
freopen("CON","r",stdin);
this code worked for me. It switches the stdin to console.
P.S: The text file used to take input before this, must be ended with a newline.

Prevent console from closing

I tried std::getchar();, cin::get(); and system ("sleep");, but nothing stops the console from closing, because it seems, that all of these functions misinterpret the pressed enter key that was supposed to confirm input for scanf. How can I prevent the console from closing with a "Press enter / any key to close" behavior after scanf? I don't want to use functions stopping the console totally from doing something for some time (like sleep) or non-portable functions (like system ("sleep")), unless such functions are the only ways.
int main () {
wchar_t *user = new wchar_t[30];
wscanf (L"%30ls", user);
// Process data... (very short time)
std::getchar ();
return 0;
}
IDE: Visual Studio 2013 (12.0) Express
I don't know the compiler. I created an empty C++ project and didn't change any settings.
Do it like this:
// do your stuff here
// prevent console from closing
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();
Use system("pause"); at the end of main() function.

Capture stdin input without echoing characters to screen

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