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;
}

Narue has dedicated a whole thread to this problem. It's worth the read.

but the simple solution would be:

cin.ignore();
cin.get();

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)

Could he also use cin.ignore() and getch() to then pause?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.