Programming: "Hello World " Program in C++

Post here about scripting and programming for HaloPC (audio, network, ai, etc.)
halo mods rock





Posts: 32
Joined: Mon Jul 26, 2004 4:54 am
Contact:

Programming: "Hello World " Program in C++

Post 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.
User avatar
Locke




Socialist Advisor Translator

Posts: 2225
Joined: Fri Aug 06, 2004 1:42 am
Location: Woot.com
Contact:

Post by Locke »

Meh, I like variety. Stickied
Image
Feel free to send me an instant message if you need anything.
GAM





Posts: 60
Joined: Fri Mar 26, 2004 10:39 pm
Location: Poop

Post by GAM »

how old are you halo mods rock?
halo mods rock





Posts: 32
Joined: Mon Jul 26, 2004 4:54 am
Contact:

Post by halo mods rock »

am 14 8)
Last edited by halo mods rock on Sun Dec 19, 2004 11:04 am, edited 1 time in total.
GAM





Posts: 60
Joined: Fri Mar 26, 2004 10:39 pm
Location: Poop

Post by GAM »

mmmk! u said in the file forums along time ago u were 10! years old
smellylobster





Posts: 104
Joined: Sun Oct 10, 2004 5:12 pm

Post by smellylobster »

yeah, I don't think a 16 year old would post a fundamental Hello World program...
MadManiak





Posts: 227
Joined: Mon Aug 30, 2004 11:10 am

Post by MadManiak »

He's not 16, he's actually like 13 or 14.
DarkBrute





Posts: 157
Joined: Wed Aug 11, 2004 12:08 pm
Location: Installation 07 Library Trying to reason with a flood about not eating me.

Post 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.
kyboren





Posts: 58
Joined: Thu Feb 12, 2004 8:49 pm

Post by kyboren »

The "using namespace std;" directive is considered bad style... it kind of defeats the whole point of using namespaces ;)
DarkBrute





Posts: 157
Joined: Wed Aug 11, 2004 12:08 pm
Location: Installation 07 Library Trying to reason with a flood about not eating me.

Post 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.
kyboren





Posts: 58
Joined: Thu Feb 12, 2004 8:49 pm

Post 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.
Last edited by kyboren on Fri Mar 25, 2005 3:01 pm, edited 1 time in total.
mopopo





Posts: 84
Joined: Wed Mar 23, 2005 5:58 pm
Contact:

Post by mopopo »

note to all of you none of your codes work!!!!try downloading notepad++ and mabey take a screenshot ok?
kyboren





Posts: 58
Joined: Thu Feb 12, 2004 8:49 pm

Post 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.
mopopo





Posts: 84
Joined: Wed Mar 23, 2005 5:58 pm
Contact:

Post 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++
kyboren





Posts: 58
Joined: Thu Feb 12, 2004 8:49 pm

Post 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.
mopopo





Posts: 84
Joined: Wed Mar 23, 2005 5:58 pm
Contact:

Post by mopopo »

it works but the formatting is wrong
mopopo





Posts: 84
Joined: Wed Mar 23, 2005 5:58 pm
Contact:

Post 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.
kyboren





Posts: 58
Joined: Thu Feb 12, 2004 8:49 pm

Post 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.
live2board





Posts: 17
Joined: Sun Mar 27, 2005 10:08 am

Post 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.
mopopo





Posts: 84
Joined: Wed Mar 23, 2005 5:58 pm
Contact:

Post by mopopo »

it runs now stop bugging me oh master at making hello world programs
Locked