Reputation: 3
I don't understand when I run this in Visual Studio 2013 why the run window goes away. So to compensate for that I put in a cin.get(); but it's still not working. Can someone explain me what I'm doing wrong and how to fix it? Mind it, I'm very new to C++.
#include <iostream>
using namespace std;
int main()
{
int a = 0;
cout << "How old are you? \n";
cin >> a;
cout << a;
cin.get();
return 0;
}
Upvotes: 0
Views: 827
Reputation: 206577
When your input is a number, the line
cin >> a;
reads the number and leaves the newline character in the input stream. When the line
cin.get();
is executed, the newline character is read and discarded. Hence, the program doesn't wait for any further input. It executes the next line, returns from main
and the program finishes.
Upvotes: 2