I’m using C++ for the first time from PHP. I was playing around with some code. To my understanding cin.get(); was suppose to stop the window from closing until I press a key, however it doesn’t seem to be working because of the code before it, I don’t know what the problem is. Here is my code:
#include <iostream>
#include <cstdlib>
using namespace std;
int multiply (int x, int y);
int main ()
{
int x;
int y;
cout << "Please enter two integers: ";
cin >> x >> y;
int total = multiply(x, y);
cout << total;
cin.get();
}
int multiply (int x, int y) {
return x*y;
}
Put a
after
>> x >> y;(or beforecin.get()).This flushes the buffer of
cinand deletes the pending\nwhich is still there, because youcinreads x and y but also reads the last return (after y). This gets read in when you callcin.get(). If you flush the buffercin.get()will see an empty buffer and everything is fine.