I've read that getchar()
should actually be preferred over _getch()
( _getch()
also needs including conio.h
). However, if I use getchar()
in this sample program...
#include <iostream>
#include <conio.h>
using namespace std;
class student {
int id;
int marks;
public:
void getdata() {
cout << "\n Enter id";
cin >> id;
cout << "\n Enter marks";
cin >> marks;
}
void putdata() {
cout << "\n" << id << "\t" << marks;
}
};
int main() {
student tom;
tom.getdata();
tom.putdata();
getchar(); // vs. _getch();
return 0;
}
..then using getchar()
won't wait for the input of a character to prevent the console window from being closed too early, which _getch()
does.
Does anyone know the reason for this? And should getchar()
really be preferred over _getch()
?
BTW, I'm using MS Visual Studio 2015.
Thanks in advance and kind regards
EDIT: I'd consider my question as not being a duplicate actually, since I was wondering about the reason for the different behavior, which had not been answered under "Is there a decent wait function in C++?", and which got clarified now.