1

I still don't know how this works with Visual Studio.

Supposedly it keeps my console open, but it doesn't. It still flashes and closes. Am I doing something wrong?

#include <iostream>
using namespace std;

int main()
{
    int a;
    cout << "Please enter an integer: ";
    cin >> a;

    if (a == 1)
    {
        cout << endl << "You typed 1.";
    }   
    else
        cout << "That's not 1.";

    cin.get();

    return 0;
}
Michael0x2a
  • 58,192
  • 30
  • 175
  • 224
yellowcain
  • 11
  • 1

1 Answers1

2

As it was already pointed out in the comments the problem is that the input buffer contains the symbol of the Enter key that is read by cin.get(); You can use either the following sequence

char c;

cin >> c;

Or before cin.get() you should call cin.ignore. For example

cin.ignore( numeric_limits<streamsize>::max(), '\n' );
cin.get();

In the last case you must include header <limits>

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335