Page 1 of 2

Programming: "Hello World " Program in C++

Posted: Fri Dec 17, 2004 2:34 pm
by halo mods rock
-------------------------------------------------------------------------------
WARNING: The following listing contains line numbers on the left. These numbers are for reference within the book. They should not be typed in to your editor. For example, in line 1 of Listing 1.1, you should enter:

Code: Select all

#include <iostream.h>

--------------------------------------------------------------------------------


Listing 1.1. HELLO.CPP, the Hello World program.

Code: Select all

1: #include <iostream.h>
2:
3: int main()
4: {
5:    cout << "Hello World!\n";
6:        return 0;
7: }


Make certain you enter this exactly as shown. Pay careful attention to the punctuation. The << in line 5 is the redirection symbol, produced on most keyboards by holding the Shift key and pressing the comma key twice. Line 5 ends with a semicolon; don't leave this off!

Also check to make sure you are following your compiler directions properly. Most compilers will link automatically, but check your documentation. If you get errors, look over your code carefully and determine how it is different from the above. If you see an error on line 1, such as cannot find file iostream.h, check your compiler documentation for directions on setting up your include path or environment variables. If you receive an error that there is no prototype for main, add the line int main(); just before line 3. You will need to add this line before the beginning of the main function in every program in this book. Most compilers don't require this, but a few do.

Your finished program will look like this:

Code: Select all

1: #include <iostream.h>
2:
3:
4: int main();
5: {
6: cout <<"Hello World!\n";
7:     return 0;
8: }
Try running HELLO.EXE; it should write

Code: Select all

Hello World!
directly to your screen. If so, congratulations! You've just entered, compiled, and run your first C++ program. It may not look like much, but almost every professional C++ programmer started out with this exact program.

Posted: Fri Dec 17, 2004 6:52 pm
by Locke
Meh, I like variety. Stickied

Posted: Fri Dec 17, 2004 6:58 pm
by GAM
how old are you halo mods rock?

Posted: Sat Dec 18, 2004 10:28 am
by halo mods rock
am 14 8)

Posted: Sat Dec 18, 2004 2:09 pm
by GAM
mmmk! u said in the file forums along time ago u were 10! years old

Posted: Sat Dec 18, 2004 2:42 pm
by smellylobster
yeah, I don't think a 16 year old would post a fundamental Hello World program...

Posted: Sat Dec 18, 2004 3:17 pm
by MadManiak
He's not 16, he's actually like 13 or 14.

Posted: Tue Mar 01, 2005 5:36 pm
by DarkBrute
Yeah, one thing. You made an error in the code. The cout funtion would not run in that way.

You need to initilize a namespace, the Standard Library Directory (or something like that)

So without the namespace, it needs to read:

Code: Select all

#include <iostream>

int main()
{
       std::cout << "Hello World" << std::endl;
       std::cin.get();
       return 0;
}
Or atleast something like that.
But with the namespace, it needs to read:

Code: Select all

#include <iostream>
using namespace std;

int main()
{
      cout << "Hello World!" << endl;
      cin.get()

      return 0;
}
Yeah... and BTW: I'm 13 and I knew C++ since I was 12.

Posted: Wed Mar 16, 2005 6:15 pm
by kyboren
The "using namespace std;" directive is considered bad style... it kind of defeats the whole point of using namespaces ;)

Posted: Sun Mar 20, 2005 2:05 pm
by DarkBrute
no, it is optional. it is not bad style. No one wants to type out <namespace>::<command> all the time. it is pointless. How is it bad style? It is time saving for most people.

Posted: Tue Mar 22, 2005 3:47 pm
by kyboren
It is bad style because of this:

Code: Select all

#include <iostream>
#include <vector>

using namespace std;


class vector{
float x;
float y;
};//a vector class that holds a vector (as it is defined in mathematics and physics) 

int main()
{ 
    vector< vector > bleh;
    //.......
}
The programmer intends to create a std::vector of his defined class vector.

With the std namespace directive, this gives one an error, and it will probably give one hours of grief before one figures out the real problem.

This also would give errors if one used a using std::vector; directive.

The ideal solution to this is simply to specify the std:: qualifier before any use of the std::vector container.

This is the entire reason namespaces were created -- to avoid confusion between two identifiers which perform different functions, but share the same name.
This is why the standard libraries were put into the std namespace -- so you could do all your own stuff without worrying about namespace pollution.

Posted: Fri Mar 25, 2005 11:58 am
by mopopo
note to all of you none of your codes work!!!!try downloading notepad++ and mabey take a screenshot ok?

Posted: Fri Mar 25, 2005 3:04 pm
by kyboren
Note to you: you are wrong. Do some research before accusing other people of being wrong.

And BTW, why would I use "Notepad++" and take a screenshot? We gave you the code. I'll stick to Emacs on Linux and MSVC++ or Dev-C++ on Windows.

Posted: Fri Mar 25, 2005 9:47 pm
by mopopo
bleh to you person im a computer programmer, and your code works but is innefficient. and DONT accuse me of being wrong because i know over 23 computer languages and im an expert in c++ and sorry :roll: that i like notepad++

Posted: Sat Mar 26, 2005 1:14 am
by kyboren
Oh, right.
I'm sorry, oh master. 23 LANGUAGES LIEK OMG.
I challenge you to name them.


Go to a C++ board and ask them why specifying the std:: qualifier is better than specifying you want to use the std namespace. Get opinions from real professional programmers on why specifying the qualifier is more acceptable.

And I never said notepad++ was bad: I simply said I'll stick to my preferred environments.

Also, here's something I noticed:
note to all of you none of your codes work!!!!try downloading notepad++ and mabey take a screenshot ok?
Compare with:
bleh to you person im a computer programmer, and your code works but is innefficient. and DONT accuse me of being wrong because i know over 23 computer languages and im an expert in c++ and sorry Rolling Eyes that i like notepad++
So, let me get this straight: You're an expert in C++, yet you can't get a "Hello World" program that works just fine to compile? Strange.

Posted: Sat Mar 26, 2005 8:38 am
by mopopo
it works but the formatting is wrong

Posted: Sat Mar 26, 2005 8:45 am
by mopopo
and the names are html, python, c, c+, c++, perl, java, actionscript, dhtml,
javascript, vbs, xml, batch, shell, fortran, pascal, css, asp, php, lua, and tex. ya i know its not 23 or whatever i said but if you want proof i can name more but i forgot some of them.

Posted: Sat Mar 26, 2005 11:23 pm
by kyboren
Sorry if I don't like to use K&R style. It's purely personal preference. Using or not using K&R is not wrong.

And guess what? You don't like the code? Use the indent program.

Posted: Sun Mar 27, 2005 10:17 am
by live2board
mopopo your a joke. All you did was you copied those languages out of the menu in Notepad++ and wrote them down, and if you were trying to make any of these programs to work in Notepad++. Good luck. Notepad++ is an editor. It can't compile programs, all it does is edit the source code. Highlights the syntax and things like that. kyboren is right, if you can't get a simple Hello World program to run and your a C++ expert than you definately need some help if you can't print 2 words to the screen. Plus all of those examples compile perfectly. BTW the run command in Notepad++ is for running .EXE, .Bat, .cmd, and .com files.

Posted: Sun Mar 27, 2005 12:29 pm
by mopopo
it runs now stop bugging me oh master at making hello world programs