getch() showing prototype error - c++

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

Related

How do I get char directly from keyboard not through buffer?

I am making a game which has a character moves in 4 directions: up, down, left, right corresponding to W,S,A,D on the keyboard. The problem is when using getch() to get input from buffer, it always has a pause time after the first single keypress. For instance, when I hold 'A' button, it acts like: A(a short period of time)AAAAAAAAA.
How do I get rid of that delay time?
Any help would be appreciated.
(Answers in either C or C++ are all acceptable, since I am using graphics.h for this program, which requires C++ to run, but I mainly code in C).
I am using windows 10 64 bits.
For a non-blocking getch method I personally use this piece of code (in C):
#include <conio.h>
int getch_noblock(){
return _kbhit() ? _getch() : -1;
}
the _kbhit() method returns 0 if no key are pressed, otherwise returns a number if the keyboard input buffer is not empty.
the _getch() method read a char from the keyboard buffer.
It works only for Windows.
Documentation:
_khbit(): https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/kbhit?view=msvc-170
_getch(): https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=msvc-170
By the way, surfing on the web I found an interesting method, but I've never tried or seen it:
https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-nolock-getwch-nolock?view=msvc-170
getch() already bypasses buffer:
Prototype
int _getch(void);
Description
_getch obtains a character from stdin. Input is unbuffered, and this
routine will return as soon as a character is available without
waiting for a carriage return. The character is not echoed to stdout.
_getch bypasses the normal buffering done by getchar and getc.
And for your problem, as someone already said, you can use the kbhit() method

How to switch back to keyboard/terminal input after using freopen() in C++? [duplicate]

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.

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.

What code should be used instead of getche in 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()

is it possible to change what the user types without pressing enter? this is a console program in C

Is this possible? The idea is typing a password and it being translated to asterisks on the fly- pretty much like a password field on HTML. I've written this code, but it converts the input AFTER the user presses enter, not while he/she is typing.
#include <stdio.h>
#include <string.h>
#include <iostream>
int main()
{
char password[11]; int i, j;
printf("Enter the password : ");
gets(password);
system("cls");
int str_len = (int)strlen(password);
printf("Enter the password : ");
for(i = 0;i<2;i++)
{
printf(" ");
for(j=0;j<str_len/2;j++)
{
printf("*");
}
}
system("PAUSE>nul");
}
I don't think it is possible using standard io. You need a library for more advanced text user interface like ncurses.
If you do want to get some extra points from the teacher, you could look into the function int getch() which does what you want. It is located in the header file conio.h.
Googling for getch should provide you with some info - MSDN and cplusplus.com are my favorite pages.
It is not directly possible using standard C, no. You typically need to convince the terminal to go into "raw" mode, in which the program is able to read each character as it's typed, i.e. not requiring enter to pressed. In the raw mode, you can also often turn off any built-in echo in the terminal, and thus get the display of typed-in characters under program control.
It might be possible to convince a terminal to do these things by outputting various escape codes, but if that is within your scope or not is hard to tell. Also, it depends on the exact terminal (and thus platform) in use.