The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > C / C++


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-14-2007, 08:24 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
C++ Hello world tutorial, every forum needs one.

Hey,

I thought for all those beginners in C++ it would help to put up the most basic tutorial, every programming site needs its own hello world tutorial after all and i have not seen one.

Ok i am using Dev-C++, if you are not using the same compiler then somethings maybe different such as your includes names etc...

First i want you to make the program before i explain it, so open up your compiler and start a new console application project, if you have not already got a .cpp file added to your project you will need to add one, normally file -> new ->file then select .cpp file.

I have called my .cpp file main as this is a name that best describes it for me, you may call it something else.

Now add the following code:
Code:
//My first program
#include <iostream>

using namespace std;

int main()
{
      cout << "Hello, World!" << endl;
      system("PAUSE");
      return 0;
}
Ok compile and run your program, your output should now be displayed on a console window

Now to break down the code so i can explain each bit.

Code:
//My first program
This line does not do anything to change the program, this is simply a comment for you, i would recommend you comment each line of you code so if you come back to it in say a year you will know what it is.

So clearly to start a comment you need to start you line with "//" if you run onto the next line you will again need to put another "//" at the start of the line. Alternatively if you have a big comment that will run onto more than one line you can use "/*" at the start of your comment (without the double quotes) this will then let you go onto many lines with no problems as long as you end you comment with "*/" examples:

Code:
//comment line one
//comment line two

/* comment that can
run onto many different
lines without problems */
Code:
#include <iostream>
This line tells the compiler to include the iosteam.h file, the iosteam contains part of the standard library (input-output part in this case) that is needed to run some of the code, such as the cout and endl in this program.

Code:
using namespace std;
This is the line that can save us a lot of typing, without this line we would have to type std::cout or std::endl, all the files in the C++ standard library declare all of its entities within the std namespace. That is why we have included this statement, normally any program where iostream is declared we declare this namespace std.

Code:
int main()
Main is the function, every program you make in C++ must contain a main() function. We use int so we can return a number at the end of the function, that explained later...

Code:
{    }
The curly braces must be used as this is where your statements go within, "{" to open and "}" to close.

Code:
cout << "Hello, World!" << endl;
This is the code that actually produces the output of Hello, World!, the first part "cout << "Hello, World!" is sending the text to the console, this text will then displayed, make sure you use the arrows facing the correct way as they are commonly mixed up with cin's arrows which face the opposite way.

The next part of the code is "<< endl;", again the arrows are sending it to the output but this will not display a message this will make a newline.

You could try replacing the code in this part with:
Code:
cout << "Hello, World!";
cout << endl;
This will still do the same job, if you would like to try removing the endl line all together you will see that the message you want to display and the system pause message is on the same line, now you can see what endl actually does

Code:
system("PAUSE");
This line will stop the console from closing, if you want to try taking this line out and recompiling it you may, you will see that without this line it will flash your console with your text and close immediately so you will not be able to see your output, with this line in it will stop it from closing and only close when you press a key or close it.

Code:
return 0;
This is where we return the number we mentioned before, if the main() returns 0 then that indicates the program has run successfully, if it returns another number then the program did not run as it should.

----------
Notice how each of the statements end with a semi-colon, this is important, without these your program will not work.
----------

I hope this tutorial has helped some of the people new to C++. Any Questions just post them and i will do my best to help out

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Lee For This Useful Post:
HelloWorld (07-14-2007)
  #2 (permalink)  
Old 07-14-2007, 08:31 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,110
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Sweet for the most basic C++ tutorial, will do some later once I'm done with ASP.NET (a long time of course..) but I probably really need to figure out rules in C++, variables, writing methods, classes, etc...

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #3 (permalink)  
Old 07-14-2007, 08:33 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Planning to make one with a couple of variables and things like that soon

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to Lee For This Useful Post:
HelloWorld (07-14-2007)
  #4 (permalink)  
Old 07-14-2007, 10:15 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,110
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Quote:
Originally Posted by Lee View Post
Planning to make one with a couple of variables and things like that soon
I'm now creating tutorial for Java reading and writing image to implement more to the web server Moreover, I also going to make it to create a report file who's hitting (IP #, When, how many times, etc) the Server

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #5 (permalink)  
Old 07-14-2007, 08:24 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Good tutorial. However, system("PAUSE") is Windows-specific. If you don't care, feel free to skip over this post.

If you care to use platform-independent code, you can instead use the following function:
Code:
void user_wait () {
    std::cin.clear(); //unset all error bits
    std::cin.sync(); //sync cin to input buffer and discard unread characters

    std::cout << "Press Enter to continue..." << std::endl;
    std::cin.get(); //wait for a character followed by Enter key

    std::cin.clear(); //unset all error bits
    std::cin.sync(); //sync cin to input buffer and discard unread characters
}
That should work well. Just place it before all of your functions, including before "int main" and then call "user_wait();" in your code (replace system("pause") with user_wait() ). The reason why it isn't simply named "pause" is because of the fact that there is a pause function declared in unistd.h on Linux. I also couldn't name it "wait" or "sleep" because those too are already Linux function names.

Edit:
You should be able to change the default source to include that. Go to DEV_CPP_INSTALL_DIR\Templates and backup ConsoleApp_cpp.txt. Open that file (the non-backup file) and copy the following to after your "using namespace std;" line:
Code:
void user_wait (void);
Add the actual function (in the codeblock above) to the end, after the "main" function. Then, replace system("PAUSE") with user_wait(). Save the file and then try creating a new project in Dev-C++.

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!

Last edited by rpgfan3233 : 07-14-2007 at 09:28 PM. Reason: Added instructions for changing the default template to simply already have user_wait ready for use. Clears buffer after use.
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-15-2007)
  #6 (permalink)  
Old 07-15-2007, 04:23 AM
Lee's Avatar
Lee Lee is offline
PT Staff*
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: Blackpool, UK
Posts: 615
iTrader: (0)
Lee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enoughLee will become famous soon enough
Thanks for that rpg, like i said its just very beginners so i wanted to keep it simple . will try to remember the code you gave though, its good

__________________

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 06:22 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50