0

am new in programming and i need your help.after writting a code if i compile and run the console will not display the output but rather shows and go immediately

2
Contributors
1
Reply
17
Views
4 Years
Discussion Span
Last Post by deceptikon
0

This is a common problem. The easiest solution is to place an input request just before the point at which your program terminates. For example:

#include <iostream>

int main()
{
    // Do something
    std::cout << "Hello, world!\n";

    // Pause for input
    std::cin.get();

    // Program terminates here
}

There are cases where that won't work, particularly if previous input from your program is still left in the input stream. You can use a magic incantation in that case and learn how it works later:

#include <ios>
#include <iostream>
#include <limits>

int main()
{
    // Do something
    std::cout << "Hello, world!\n";

    // Clear extranous input
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

    // Pause for input
    std::cin.get();

    // Program terminates here
}

Of course, if there's no leftover input, you'll have to hit the [Enter] key twice because cin.ignore blocks for input too.

Edited by deceptikon

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.