My C ++ code compiles, but becomes an awkward program that doesn't work

I wrote a CPP program on Code :: Blocks and compiled it (MinGW). This is a simple application, but it shows a big problem that I still can not solve.

#include <iostream> int main(int argc, char *argv[]) { std::cout << "Something here"; return 0; } 

When I try to run this program (on code blocks [F9 - compile and run]), I get this as a result: Console

Nothing happens...

If I execute it from code blocks, it just opens and disappears. But in two cases this is inconvenient, I can not kill this process. But when I try to restart the computer, I get an error message, it says that the program was incorrectly initialized. I don’t know how to debug programs, and I have no idea how to proceed. could you help me?

Sorry for any language errors, I am not an English master (yet). Thanks.

Edit:

 #include <iostream> int main(int argc, char *argv[]) { std::cout << "Something here" << std::flush; return 0; } 

It still does not work. Even with '\n' or std::endl .

+5
source share
2 answers

You are not std::flush your output, so "Something here" is stuck in the internal std::cout buffer and is not printed until the end of your program.

To fix this, you can (choose one):

  • std::cout << "Something here" << std::endl;
  • std::cout << "Something here\n";
  • std::cout << "Something here" << std::flush;
+3
source

Thanks to Bo Persson (who commented on my post) I fixed it by disabling my antivirus (Avast). Thanks!

Edit:

std::cout << "Something here"; (without std::flush ) also works after disabling Avast.

+3
source

Source: https://habr.com/ru/post/1239579/


All Articles