Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My C++ code compiles but becomes a unkillable program that doesn't work [duplicate]

I wrote a CPP program on Code::Blocks and compiled it (MinGW). It is a simple application but shows a big problem that I still can't 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 result: Console

Nothing happens...

If I execute it out from codeblocks, it just opens and disapears. But on the two cases, it is unkillable, I cant kill this proccess. But when I try to restart the computer, I get an error, it says that the program wasn't correctly initialized. I don't know how to debug programs, and I have no idea of how to proceed. Could you help me?

Sorry for any language mistakes, I'm not an english master (yet). Thank you.

Edit:

#include <iostream>

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

It stills not working. Even with '\n' or std::endl.

like image 694
Tartaruga Mágica Avatar asked Dec 31 '15 14:12

Tartaruga Mágica


2 Answers

You didn't std::flush your output, so the "Something here" is stuck on the internal buffer of std::cout and is not printed before your program ends.

To fix that, you can (pick one):

  • std::cout << "Something here" << std::endl;
  • std::cout << "Something here\n";
  • std::cout << "Something here" << std::flush;
like image 188
YSC Avatar answered Oct 11 '22 00:10

YSC


Thanks to Bo Persson (that commented at my post), I fixed it by turning off my antivirus (Avast). Thank you!

Edit:

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

like image 34
Tartaruga Mágica Avatar answered Oct 10 '22 22:10

Tartaruga Mágica