cout doesn't display anything in terminal
g++
is a compiler. It turns your source code into an executable program, but doesn't run it.
You must run the program yourself. The default name of the program generated by g++
is a.out
(for historical reasons), so you would run it as
$ ./a.out
If you want to choose a different name for your program, you use the -o
option:
$ g++ Code.cpp -o myProgram
$ ./myProgram
But here's how I would write your program:
#include <iostream>
int main() {
std::cout << "Hello World\n";
return 0;
}
See here and here for some reasons.
John Spencer
Updated on June 08, 2022Comments
-
John Spencer almost 2 years
I'm just trying to get my c++ code to output properly in terminal on my mac, but it doesn't show anything. I'm using xcode as a text editor, saving the file as Code.cpp, and then typing g++ Code.cpp into terminal. Before it was showing errors when my code had bugs, but now that it runs correctly it doesn't show any output. Any thoughts? Here's my code:
#include <iostream> using namespace std; int main() { cout << "Hello World" << endl; return 0; }
Here's what I put into terminal, and it just skips down to the next line without the "Hello World" output.
jspencer$ g++ Code.cpp jspencer$
Thanks in advance for the help!!
-
user1502952 over 10 yearsDude don't paste answers for such question. Settle them in the comment section itself :).
-
BoBTFish over 10 years@user1502952 I often see "bad" questions that don't deserve an answer, but should instead be closed. However, this one has been left open, so should get an answer. If you know of a duplicate, please vote to close it as such and I will gladly remove my answer and vote to close as well. (Actually, I guess you can't vote to close with low rep. If there is a duplicate please post it in the comments.)
-
John Spencer over 10 yearsI know I'm a noob, it was a stupid question. I can remove it if that would be better.
-
BoBTFish over 10 yearsI'm happy for it to stand. It's a well written question with an appropriate level of detail. It hasn't been closed as a duplicate, so it should get an answer. Doesn't matter that many would consider it very simple. It may be arguable it is off topic, but we generally accept questions about compilers, tools, etc (I was shouted down about what I consider a much more dubious question earlier today, although it has since been closed by others).
-
mvw over 10 yearsI like the question, it is not that unusual, if you start developing on a unix like system.