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?
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.
There are several ways to watch the output of a small program that finishes very quickly:
Put an infinite loop while(true)
, or for(;;)
,
Read from std::cin
.
Put a break point in your debugger just before the exit from main()
.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.