0

i m starting to learn c++.
i m trying to do a console app.
this is my code but i encountered a problem with it
after i key in num1 and num2
the total will be display for about a few mili seconds and the console terminate itself.
if i add cin.ignore(2) at the end of the code, the console will not terminate itself.
however it i leave the bracket empty, same situation happens again...
i am using dev c++
thanks for helping

#include <iostream>
using namespace std
int main()
{
    int num1,num2;
    cout << "Please enter num1 : ";
    cin >> num1 ;
    cout << "Please enter num2 : ";
    cin >> num2 ;
    cout << "Total of num1 + num2 : " << num1 + num2 << endl;
    return 0;
}
4
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by Nick Evan
0

The reason is because entering numbers leaves the '\n' (Enter key) in the keyboard buffer. cin.ignore() will just remove the '\n'. So you either need another cin.ignore() or do what you did and use cin.ignore(2)

0

getch() is a non-standard function. It came with the conio.h header. (Turbo C for example). It is no longer supported in new compilers, so it would be a bad idea to use it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.