2

I'm new to C++. I was messing around creating a simple program that took the two numbers you inputted and displayed the answer. The only problem was that it closed too fast. I decided to do what I usually do and and a cin.get(); and it usually solves the problem. The weird thing is, this time it didn't. I had to put two cin.get(); statements. I'm curious to why it requires me to put two for it to stay open instead of the usual one. Here is my code:

int a;
    cin >> a;
    int b;
    cin >> b;
    cout << a + b;
    cin.get();
    cin.get();
Meme Machine
  • 949
  • 3
  • 14
  • 28

2 Answers2

4

At the last input (b value) you are also giving a new line as input (when you press enter). So the first cin.get() is taking that new line as another input. And the last one then keeps your console open.

Adding cin.ignore(); after cin>>b; should solve the problem.

Tahlil
  • 2,680
  • 6
  • 43
  • 84
1

Use cin.ignore(); instead. That should fix your problem.