cout doesn't display anything in terminal
Asked Answered
E

1

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

Entebbe answered 30/8, 2013 at 15:28 Comment(9)
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;.Subscript
@Subscript my thought as well.Wrought
Thank you very much! So what are the evils of "using namespace std;"? is it better to just use std::cout, etc?Entebbe
It's a link, but I'll paste it again: 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) either, but people have mixed feelings about that one.Subscript
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.Subscript
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.Entebbe
I've heard good things about it, but I'd be quite disappointed if it was teaching you to use using namespace std;.Subscript
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!Entebbe
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.Subscript
S
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.

Subscript answered 30/8, 2013 at 15:45 Comment(5)
Dude don't paste answers for such question. Settle them in the comment section itself :).Paralipomena
@Paralipomena 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.)Subscript
I know I'm a noob, it was a stupid question. I can remove it if that would be better.Entebbe
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 earlier today, although it has since been closed by others).Subscript
I like the question, it is not that unusual, if you start developing on a unix like system.Wrought

© 2022 - 2024 — McMap. All rights reserved.