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
|