getchar() in program from lecture

Hello everyone,

I've recently started with a c++ course in uni. We've got the following program:

#include <iostream>
using namespace std;
int main ()
{
int a, b, prod;
cout << "Geben Sie zwei ganze Zahlen ein: ";
cin >> a >> b;
prod = a * b;
cout << "Das Produkt von " << a << " und " << b
<< " ist " << prod << endl;
getchar ();
getchar ();
return 0;
}



Now, this program only compiles if I add #include <stdio.h> otherwise it doesn't recognize getchar.
Was that just an error in the program or is there a way it would compile without stdio.h?

Also, what is the getchar() even good for? If I just take it out, it compiles just fine and works as it is supposed to.
So, is there a reason for this getchar?

-Curious
It's here because the original writer fears people will run the program from within an IDE, and come back with the complaint in this thread:http://www.cplusplus.com/forum/beginner/1988/

Here is a whole thread about just this case: http://stackoverflow.com/questions/1432989/why-is-my-professor-using-two-getchar



Oh, I see. Thank you for the links!

So the missing #include <stdio.h> was probably just a mistake? Or is it possible to get getchar() to work with just the code I posted above?
the missing #include <stdio.h> was probably just a mistake? Or is it possible to get getchar() to work with just the code I posted above

On some implementation, it's quite possible that the header <iostream> has the line #include <stdio.h> somewhere inside, or inside another header it includes. The standard headers are allowed to include other standard headers, but to be portable, you should include all of the ones you're using.
Topic archived. No new replies allowed.