0

I'm getting started with C++ and am trying to work with vectors and printing stuff. I am using Windows 10, writing my code in Visual Studio Code, I just downloaded the MinGW-g++ compiler, and am trying to run my code in CMD.

This is my code, I've tried printing different things, and it's not even printing this "Hello". I've also tried using << endl; which also prints nothing. I also tried adding and remove a "return 0;" at the end but it changed nothing. The program compiles just fine. Any advice?

#include <iostream>
using namespace std;
int main(void)
{
    std::cout << "Hello";
    std::cout.flush();
}
3D_Dev
  • 1
  • Double-click the program in the explorer, see if it shows any errors. – HolyBlackCat Nov 04 '21 at 11:33
  • open a cmd.exe window and navigate to the folder with the compiled executable. Then run it from the command line and see what happens. – L. Scott Johnson Nov 04 '21 at 11:37
  • 2
    Drop the void in `int main(void)` - you are not passing anything, so there is no need for that. Also drop `using namespace std;` entirely, having it is not really a good practice and you are using `std::cout` anyway. Do put `return 0;` at the end of `main` as `main` must return something. Side note: it's entirely possible that your program runs to fast for you to see any output if you are launching it from VS directy. As it was mentioned - navigate through cmd to folder with compiled executable and launch it – Eugene Anisiutkin Nov 04 '21 at 11:42
  • 1
    `main` is explicitly marked in the standard to have an automatic `return 0;` at the end, so it's just not necessary to be explicit here. – Bartek Banachewicz Nov 04 '21 at 11:49
  • Explaining @EugeneAnisiutkin: 1. [`using namespace std`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) 2. Having void as function arguments is allowed in C++ for compatibility reasons with C (because there you need to specify to accept no arguments at all while in contrast empty parentheses could accept *anything*, a legacy from pre-ANSI C) – you couldn't include most C headers otherwise. Specifying `void` in pure C++ it's considered bad practice, though. – Aconcagua Nov 04 '21 at 11:53
  • 3. `main` is an exceptional case, sole function with return value allowed not to return anything (0 is implicitly returned then). Not returning anything is often considered bad practice, though. – Aconcagua Nov 04 '21 at 11:53
  • Side note: Usually programmes terminate their output with a newline so that the next console prompt starts on its own line (some terminals do so anyway, others not). So you could have `std::cout << "hello\n";` – or equivalently, but maybe more elegant: `std::cout << "hello" << std::endl;` and dropping the explicit call to `flush` (`std::endl` does so implicitly – which means that if you don't need to flush as other output follows anyway you should rather `<< '\n'` instead or include the newline in a string literal if previous output is one, as in `"hello\n"`). – Aconcagua Nov 04 '21 at 12:02
  • Your program is fine. You most likely have a misconfigured environment. Please show how you compile the program. – n. m. could be an AI Nov 04 '21 at 21:31

0 Answers0