5

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.

  • 1
    I didn't encounter the problem you mentioned, and I copy pasted your code. So obviously there is no problem with the code. Something else is causing this strange behavior. Can be something related with the compiler settings of your Code::Blocks (I too compiled and ran the program on my code::blocks using F9) – incomplet_ Dec 31 '15 at 14:31
  • 9
    The most common reason for [a blank console window](http://stackoverflow.com/questions/33690697/running-my-c-code-gives-me-a-blank-console) is using Avast anti virus. – Bo Persson Dec 31 '15 at 14:36
  • @Bo Persson Thank you, the problem was the Avast. – Tartaruga Mágica Dec 31 '15 at 19:53

2 Answers2

3

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;
YSC
  • 38,212
  • 9
  • 96
  • 149
  • 2
    `std::cout` buffer is flushed as part of its destruction. It might have something to do with the POSIX requirements to have an actual endline symbol at the end of the line. – Revolver_Ocelot Dec 31 '15 at 14:26
  • @Revolver_Ocelot I can't find an authoritative source saying `std::cout` is flushed upon destruction. Can you link one? – YSC Dec 31 '15 at 14:30
  • @YSC Is [this](http://en.cppreference.com/w/cpp/io/ios_base/Init) good enough source? – Algirdas Preidžius Dec 31 '15 at 14:38
  • Quick search: C++ standard 27.4.2/3 _`ostream cout;`The object `cout` controls output to a stream buffer associated with the object stdout, declared in ``_. I do not have a copy of C standard, but [this answer](http://stackoverflow.com/questions/17230411/printf-flush-at-program-exit) says that `stdout` is flushed at program exit. And 3.6.1/5 says that "_A return statement in main has the effect of leaving the main function [...] and calling std::exit with the return value as the argument._" – Revolver_Ocelot Dec 31 '15 at 14:38
  • Thanks to both of you. It seems the fault comes from the system's console. Anyway, both `std::flush` and the new line work. – YSC Dec 31 '15 at 17:16
3

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.