1

I tried searching for similar problems in here but all of them were too complex, I'm just starting out with C++ doing my Hello World, here's the code, just in case:

#include <iostream>

int main() {
std::cout << "Hello World!\n";
return 0;
}

It only works fine if I compile it then start without debugging and without rebuilding it (when it says it's out of date). If I start debugging it still says it's out of date but no matter if I rebuild it or not, the console shows up for like half a second then the program exits. Why is this?

Als
  • 11
  • 1
  • did you check http://stackoverflow.com/a/2853912/819272 – TemplateRex Apr 13 '13 at 07:48
  • Yes, thing is I don't have any header files, nothing else than my source file... – Als Apr 13 '13 at 07:57
  • 1
    the program exits because it finished running. if you want to stop, you could add a breakpoint in the debugger at the `return 0;` line (which is not necessary anyway for correctness, because it's implicit) – TemplateRex Apr 13 '13 at 08:06

2 Answers2

1

it is because your program executes then finishes through your

return 0;

so it happens and finishes almost instantly you need a way to be able to "pause" your applications execution to see your ouput. you could do std::cin >>. but i would recomend the use of system pause all you need to add is

System("pause");

and the

#include <stdlib.h>

so your hello world applicaiton should look like

#include <iostream>
#include <stdlib.h>
int main()
{
   std::cout << "hello world\n";
   system("pause");
   return 0;
}

however

 system("pause")

is a windows specific feature and should be avoided for serious applications for various reasons.

Zanven
  • 144
  • 6
  • Makes sense, however the only approach I said would work with me opens the window and doesn't exit but pauses waiting for the user to press any key to exit. So if it already does that do I really need to add something? Besides it's just one problem - it still says that the project is out of date for absolutely no reason. I don't know, really - total noob... – Als Apr 13 '13 at 08:24
  • can you be more accurate about how its telling you the project is out of date? warnings when you compile?? if so paste the message from your output view. i personally havent fully transioned to vs2012 and still mostly use 2010. but i will still try to help. – Zanven Apr 13 '13 at 10:11
0

There are several ways to watch the output of a small program that finishes very quickly:

  1. Put an infinite loop while(true), or for(;;),

  2. Read from std::cin.

  3. Put a break point in your debugger just before the exit from main().

  4. Redirect your output from the command line: my_program.exe > my_output.txt.

I prefer method no. 4 because it doesn't require changing your code.

TemplateRex
  • 69,038
  • 19
  • 164
  • 304