article

A Beginners guide to C++

Email
Submitted on: 1/5/2015 10:55:00 PM
By: James Dunne (from psc cd)  
Level: Beginner
User Rating: By 7 Users
Compatibility: C++ (general)
Views: 1464
 
     This article teaches beginners a few things, not all but I am gonna include in other tutorials, I will only make a sequel to tutorials if you like the prequal

 
				

Introduction -

Okay, This is my first tutorial ive ever written for Planet-Source-Code so go light if I make mistakes :). I really hope this helps you as much as another tutorial helped me. In this tutorial you will learn (hopefully) how to compile source code and write a basic program that will display an Variable that you input.

Enjoy, ~Jamesy.

Part 1 : The first code of a beginner-

Well, if you dont have a clue what to do you probably haven't got
compiler yet so go to http://www.download.com and search Dev-C++, this will show up a single result, if not search for Bloodshed Dev-C++. This is a C++ and a C compiler also! So go ahead download it. Now, once you have installed it open it and click the new file icon to bring up a text editor-like window with this inside :

#include
#include

int main()
{

system("PAUSE");
return 0;
}

now, in between the sharp brackets (ones with a point in the middle) type or copy this :

cout << "Howdy there, Oh yeah, Hello World!";

Now you should have something like this :

#include
#include

int main()
{
cout << "Howdy there, Oh yeah, Hello World!";
system("PAUSE");
return 0;
}

Go to "Execute" on the toolbar (where File, Edit, etc is) then press "Compile & Run" and it should say in an MS-DOS prompt :

Hello there, Oh yeah, Hello World!

Congrats, you just programmed in C++! Sure you didnt write the program but you did compile it which means your a programmer.

Now for the explanation :

#include means to insert code out of a header file.

iostream.h is the file that lets you do the cout and cin commands. iostream means input-output-stream just if you are wondering.

stdlib.h lets you do the system("PAUSE"); command.

The blank line is just whitespace, ignore it but put it between every function.

int main() is a function that must be in every C++ program. Its like your motherboard to your PC, It cant be run with out it.

{ is the opening bracket and just for time keeping, } is the closing bracket.

cout prints to the console (<< must be present, cant be the other way round)

system("PAUSE"); stops the script from running after it, making the program to stop after it has printed to the console instead of printing then closing. Thank this for giving you the pleasure on seeing your pride joy in your first C++ program.

return 0; returns 0 to close the program.

On to Part 2 : The Juicey Stuff

Part 2 : The Juicey Stuff

Now click the New Code button again. Now we are gonna stick our fork in the steak this time and not the chips.

Click on the first whitespace and press enter then type :

int clouds;

then in the next white space put :

cout << "How many clouds do you see?\n";
cin >> clouds;
cout "\nThere are " << clouds << " in the sky!\n";

Now you should have something like this :

#include
#include

int clouds;
int main()
{
cout << "How many clouds are there?\n";
cin >> clouds;
cout << "\nThere are " << clouds << " in the sky!";
system("PAUSE");
return 0;
}

Now compiler and run and test :)

Now for the explanation :

*Note : I am only explaining things you have not already learnt


int clouds; is the variable, this stores the information you type in when you ran the program. Say for instance 86 its would say :

There are 86 clouds in the sky!

There are more types of Variable which I will explain in a later tutorial if you like my tutorial.

<< clouds << in the cout command basically displays the variable. You can do this with any variable and is used when using endl (another way to linebreak, I prefer \n for programs like these) You can even add, subtract, multiply, etc with it.

Ok now you have enough knowledge to add to the program and make it more instresting and stuff, after my next tutorial you will be able to make a Questionaire!

Other things!

Ok, if you want to get in contact with me, I dont use email, Only AIM! Contact me on chickenscooop if you need anything or want to recommend something.

Again, I would like feedback, please give me feedback :)

~James Dunne



Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.