8

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!!

mvw
  • 5,075
  • 1
  • 28
  • 34
John Spencer
  • 83
  • 1
  • 1
  • 4
  • 3
    That just compiles the program. Then you need to run it, the default name is probably `a.out`, so run it as `./a.out`. If you want to compile with some other name, use the `-o` option, such as `g++ code.cpp -o myexecutable`. Also, can I grab you while you are just getting started, and warn you about the evils of [`using namespace std;`](http://www.parashift.com/c++-faq/using-namespace-std.html). – BoBTFish Aug 30 '13 at 15:30
  • @BoBTFish my thought as well. – mvw Aug 30 '13 at 15:32
  • Thank you very much! So what are the evils of "using namespace std;"? is it better to just use std::cout, etc? – John Spencer Aug 30 '13 at 15:35
  • It's a link, but I'll paste it again: http://www.parashift.com/c++-faq/using-namespace-std.html Unfortunately it is a mistake made in an awful lot of bad beginners tutorials. I don't much like [`endl` (also a link)](http://stackoverflow.com/a/5492605/1171191) either, but people have mixed feelings about that one. – BoBTFish Aug 30 '13 at 15:38
  • In fact, may I ask where you are learning from? There are many very bad sources, so I will point you to our [book list](http://stackoverflow.com/q/388242/1171191). – BoBTFish Aug 30 '13 at 15:42
  • Great list, and yes I'm using The C++ Programming Language (Bjarne Stroustrup), which seems to be a favorite. It's the text for my CS 103 course I just started this semester. Seems like learning from the creator of the language is the way to go. – John Spencer Aug 30 '13 at 15:46
  • I've heard good things about it, but I'd be quite disappointed if it was teaching you to use `using namespace std;`. – BoBTFish Aug 30 '13 at 15:49
  • I don't think it does, but my CS teacher is the one who told me to use using namespace std; So I appreciate the advice! – John Spencer Aug 30 '13 at 15:51
  • 1
    Unfortunately we see a lot of new people here getting bad advice from teachers. I think many teachers rarely write "real" `c++`, just teaching the same thing once a year and never un-learning their own bad habits. – BoBTFish Aug 30 '13 at 16:12

1 Answers1

9

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.

Community
  • 1
  • 1
BoBTFish
  • 19,167
  • 3
  • 49
  • 76
  • Dude don't paste answers for such question. Settle them in the comment section itself :). – user1502952 Aug 30 '13 at 15:46
  • @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.) – BoBTFish Aug 30 '13 at 15:51
  • I know I'm a noob, it was a stupid question. I can remove it if that would be better. – John Spencer Aug 30 '13 at 15:57
  • 1
    I'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](http://stackoverflow.com/q/18534089/1171191) earlier today, although it has since been closed by others). – BoBTFish Aug 30 '13 at 16:02
  • 1
    I like the question, it is not that unusual, if you start developing on a unix like system. – mvw Aug 30 '13 at 16:28