problem with opening hello world

hello

I have got a problem, i wrote the first program hello world. and it is correct because it open's but the problem is: it open's is ms dos for less than a second. and the second program from the manual on this site is the same problem. so can anybody help me??

Jerome

I am not verry good in English so sorrie for all the mistakes
closed account (S6k9GNh0)
Hi kitekeeter! This is a problem for a lot of noobies! What happens is when you start a program, it calls the starting function (main()). When it does this it processes what you tell it to process (in your case, you send Hello World to the console). After it's done processing, it ends the function and once the main() function ends, the application itself ends.

In other words, you need to somehow stop the function from ending to fast. A popular way is to place a function that uses an interrupt in the function. A popular choice is the C STD, getchar();

1
2
3
4
5
6
7
8
#include <iostream>

int main(int argc, char ** argv)
{
    std::cout << "Hello world! Nice weather, eh?" << std::endl;
    std::cout << "Press any key to continue...";
    getchar();
}


This is my personal way of doing it. There's a tutorial on this site explaining how and why this works.

http://cplusplus.com/forum/articles/7312/
Last edited on
I would only quibble with the phrasing of line 6 -- after all, any key doesn't work, just Enter. Hence:

6 std::cout << "Press ENTER to continue...";

Hope this helps.
Last edited on
hey computerquid and duoas

thanks for your help, it works verry good. this is the first time I am programming. so that explain's why i run in to this problem.

Last edited on
Topic archived. No new replies allowed.