Skip to main content
GameDev.net gamedev.net
🔒 Locked

C plus plus hello world compiles but doesn't seem to run

Started by Evil Booger Nov 21, 2009 at 1:35 PM 12 replies 2.9k views
Original Post
Evil Booger
Evil Booger
Hey, I'm trying to build and run a hello world application from command prompt. I have run it successfully from the visual studio 2008 EE and Xcode ides, but when I try to run it from either command prompt, or terminal, it compiles but I don't see the actual "Hello World". I don't think its anything wrong with the code, I'm pretty good with C plus plus AND I copied and pasted it:

#include <iostream>

int main (int argc, char * const argv[]) {
    // insert code here...
    std::cout << "Hello, World!\n";
    return 0;
}

lord_balron
lord_balron
Put in a std::cin.get() just before the return 0;
TheUnbeliever
TheUnbeliever
Press Ctrl+F5 - 'run without debugging'. This will automatically pause your program on completion.

Quote:
Original post by lord_balron
Put in a std::cin.get() just before the return 0;


Please don't. This often ends up causing more difficulty than it's worth.
[TheUnbeliever]
jouley
jouley
Quote:
Original post by TheUnbeliever
Press Ctrl+F5 - 'run without debugging'. This will automatically pause your program on completion.

Evil Booger can "run it successfully from the visual studio 2008 EE and Xcode ides," so I don't think that's the problem. The problem is running it from the command line.

Evil Booger - can you give us details on this? When you say "it compiles but I don't see the actual 'Hello World'," I wonder if you're actually running the program. Compiling and running are two different steps that the IDE often does for you (via (Ctrl+)F5 in Visual Studio, for example). What commands do you use?

Quote:
Quote:
Original post by lord_balron
Put in a std::cin.get() just before the return 0;

Please don't. This often ends up causing more difficulty than it's worth.

Seconded.
Evil Booger
Evil Booger
Well, I'm on my mac right now, so I typed "g++ Practice.cpp" after cd-ing to the directory. And because it goes to the next line I assumed that it compiled with no errors.
jouley
jouley
Quote:
Original post by Evil Booger
Well, I'm on my mac right now, so I typed "g++ Practice.cpp" after cd-ing to the directory. And because it goes to the next line I assumed that it compiled with no errors.

Yup, it sounds like you've compiled it, but you still have to run it. g++ will create the executable in the same directory by default, likely called "a.out" which you have to execute. Simply type ./a.out (or whatever the output file is called) in the terminal, it should run the program.

To change the name of the output file, add the following to your compilation step: "-o [outputfile]". Then just run "./[outputfile]" from the command line.
Evil Booger
Evil Booger
Oh, I had no idea about that last step, thanks, it works now.
TheUnbeliever
TheUnbeliever
Quote:
Original post by jouley
Evil Booger can "run it successfully from the visual studio 2008 EE and Xcode ides," so I don't think that's the problem.


Whoops, reading comprehension: nil.
[TheUnbeliever]
Sneftel
Sneftel
Moved to For Beginners.
steven31dd
steven31dd
I usually use system("PAUSE"); before the exit code.
I love that adrenaline feeling when I pwn you.....
nobodynews
nobodynews
Quote:
Original post by steven31dd
I usually use system("PAUSE"); before the exit code.


That's worse than using cin.get().
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
jouley
jouley
Quote:
Original post by nobodynews
Quote:
Original post by steven31dd
I usually use system("PAUSE"); before the exit code.


That's worse than using cin.get().

I know I've dealt with this question a few times, but in my searching for an old answer to "why does my program disappear?," I came up with nobodynews's own response at the bottom of this thread, which includes a breakdown of some right and some wrong ways of fixing it. Just to clarify this one for those who may come across this later and wonder...
clashie
clashie
What about

std::sync();std::ignore();


I think this should clear the input buffer as well. Sounds better than std::cin.get()
OneThreeThreeSeven
OneThreeThreeSeven
run your programs from the command line. Never have to worry about adding pauses and all that crap.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.