cout to terminal

Hi all,
I'm trying to go through the tutorial found on c++.com and the first thing is creating the classic Hello World program. I am using Ubuntu, the Bash shell terminal, and the g++ compiler. When I run my program at the terminal, I don't ever see 'Hello World'. cout appears to print to the terminal by default so I'm not sure what my problem is. Here is my code:

1
2
3
4
5
6
7
8
9
10
//My first program

#include <iostream>
using namespace std;

int main()
{
	cout << "Hello World!";
	return 0;
}


Thanks for the help! Also, how do you search a specific forum here? The search field above seems to search all of cplusplus...
Last edited on
Hmm, none of these seem to correct the issue. That thread talks about the terminal opening and immediately closing. I never see a terminal open though. At the terminal in the same directory as my program, I type:

$ g++ tut.c++

This runs the following code (now with inputs, as suggested):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//My first program

#include <iostream>
using namespace std;

int main()
{
	int i;
	cout << "Hello World!";
	cout << "press ENTER";
	cin >> i;
	cin.get();
	cin.get();
	return 0;
}


I don't see anything in terminal though...
$ g++ tut.c++

This wouldn't run your program, it would only compile it. I'm not even sure if that links it or not.

There's actually 2 steps that need to be done:
1) Compile each source file. This produces "object" files (I think they're .o in gcc)
2) Link all the object files together to form the final executable binary

Once you have the binary, then you can run it from the terminal, THEN you'll get that expected output.


Of course, I highly recommend you get an IDE and don't use gcc from the command line directly like that. It's MUCH easier to just press a "build" button and have everything automatically done for you. IDEs also make debugging way easier.

There are free IDEs available in Linux repositories. When I was running Ubuntu I used Code::Blocks. Maybe give that a try.
works in codeblocks

see video

http://screencast.com/t/dVKcsqJ2U
Oh, of course! Thank you very much for pointing me in the right direction, this will save a lot of headache.
This kinda sounds as if you'd misunderstood C++ for a scripting language...
Topic archived. No new replies allowed.