Getchar Wont Stop the program!

Jul 19, 2012 at 8:40am
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 Python)

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

}
Jul 19, 2012 at 9:19am
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 Jul 19, 2012 at 9:25am
Jul 19, 2012 at 11:13am
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.