I have just started today. I put the following code into DEV-C++:

#include <iostream>
int main()
{
    std::cout << "Enter two numbers:" << std::endl;
    int v1, v2;
    std::cin >> v1 >> v2;
    std::cout << "The sum of " << v1 << " and " << v2
              << " is " << v1 + v2 << std::endl;
    return 0;
}

and press compile and run. The program executes but once i've put my 2 numbers in and press 'enter' the program window shuts immediately. Is there any way to change settings so that i can see the output and the window doesn't shut?

Thanks, and sorry for asking the most basic of questions... i will of course be programming flights to Mars this time next year....

Add this to your code (just before return 0):

std::cin.ignore();
std::cin.get();

The first line discards the '\n' that's still on the stream. The second keeps your window open.

The easiest way is to block for input using cin.get(); . However, that only works if there isn't anything left in the stream, which in this case is likely to happen. Change your code to this:

#include <iostream>
int main()
{
    std::cout << "Enter two numbers:" << std::endl;
    int v1, v2;
    std::cin >> v1 >> v2;
    std::cout << "The sum of " << v1 << " and " << v2
              << " is " << v1 + v2 << std::endl;
    std::cin.ignore ( 80, '\n' );
    std::cin.get();
    return 0;
}

This call to cin.ignore reads and discards up to 80 characters or until a '\n' character is found. That clears up the stream so that the next cin.get call will stop the window from closing.

Thank you

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.