Getchar Wont Stop the program!

Hey Guys, I was coding a simple program and i used getchar to stop the program from closing. But when i ran it it didnt stop, and then i tried getch and it did. I know i can just use getch for stopping it but im wondering why isnt getchar working? thanks, heres my code (and im using visual c++)

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
cout << "What is your favourite number?";
cout << endl;
int FavNumber;
cin >> FavNumber;
cout << "You favourite number is " << FavNumber;
cout << " !!!";
cout << endl;
getchar();
return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
cout << "What is your favourite number?";
cout << endl;
int FavNumber;
cin >> FavNumber;
cout << "You favourite number is " << FavNumber;
cout << " !!!";
cout << endl;
getchar();
getchar();
return 0;

} 
Last edited on
Because when you press Return to confirm the value of FavNumber, getchar takes that return input. You can use getch, or you can clear the input buffer before calling getchar. Anyways, wrong category, this isn't Windows-only.
Topic archived. No new replies allowed.