Mohit A.
Mohit A.

Reputation: 3

C++, cin.get(); not working

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

Answers (2)

Nana Yaw
Nana Yaw

Reputation: 38

you can use system("PAUSE") after cout.

Upvotes: 0

R Sahu
R Sahu

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

Related Questions