This tutorial has been made for new c++ programmers. Unfortunately their are many great minds out there but they don't have access to free and quality c++ tutorials.
Terms of Agreement:
By using this article, you agree to the following terms...
1) You may use
this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
2) You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.
3) You may link to this article from another website, but ONLY if it is not wrapped in a frame.
4) You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
Style: In the
first tutorial some people had pointed out that I had not declared main()
to return void, this is no longer a standard expected from programmers.
Get main() to return int & simply return
0; as the last line in main().
<-- content_start-->
Note:
According to the latest standards <iostream.h> is
not required it can be simply written as <iostream>,
with the standard code that shall be used through out the
tutorial (I hope!).
If the standard code in the tutorial does not work
on your compiler and you still wish to use <iostream>
instead of <iostream.h> just add a line
using namespace
std;
& if this does not work you have an old
compiler which cannot work with these standards.
<-- Begin Search Code -->
- Preprocessor
Preprocessing is done when the #include command is
used to add header files. The actual preprocessing is
done much before the compiler reaches the code
section of your program. The #include command is one
of the possible actions of preprocessing.
All Preprocessing is done with #(Hash/Pound Symbol)
- #include
This a directive to the preprocessor, with the help
of the " Include Hash/Pound " the file after
the directive s read.
Eg: #include <filename>
or #include <filename.h>
- IO
When generally the topic of input is raised, the
first thought that comes to a person's mind is that
input means input from a keyboard, mouse, joystick,
etc and is outputted to a device such as a disk, monitor
(screen) or printer & this thought is technically
correct; in explaining the concept of input-output.
- Input is done in streams of bytes or a sequence
from devices (e.g.. keyboard, mouse, joystick,
etc).
-In Output bytes flow from the output devices.
A program is divided into bits, bytes, a byte may
represent ASCII characters.
The C++ iostream library has many I/O capabilities
but for now we are only concerned with a few.
The <iostream> header file defines the
cin, cout objects.
I think the theoretical
explanation of the beginning of a program should be
enough for now. Let us now get to the 'real' juicy
part of programming. (Duh - Writing the code)
- 2 Input/Output Objects ( In
relation to the tutorial)
cout (It ain't pronounced as Cowt, its
pronounced as C Out)
The cout operator has already been explained in tutorial
1 but here is a brief review + additions.
cout is a keyword, which is exclusively reserved
for C++ defined purposes. It is used to output data
info to the standard output device.
cout<<"This is Text";
By the way Comments(
// ) have been explained previously.&nbs;
// p;
// << is known as
stream insertion operator.
// "This is Text"
is displayed to the screen.
//The << is also known
as left shift operator, it is used after the cout
command.
cout<<text;
//The data from a variable
text is displayed to the screen.
//Variables have not yet been
explained at this point.
Now to explain the next topic cin & datatype (int), we shall
attempt to understand by studying a simple program. - Adding 2
integers
Non
Standard Version
#include <iostream>
int main()
{
int one, int two,
int sum;
cout<<"Please enter the first number"; //Requests for first integer
cin>>one; //accepts first integer
cout<<"Please enter the second number"; //Requests for second integer
cin>>two; //accepts second integer.
sum = one + two; // one & two get added and the value gets assigned to sum.
cout<<"The sum:"<<sum; //sum of 2 int's is displayed
return 0; //program has ended
}
Standard
Version
#include <iostream>
using std::cout;
using std::cin;
int main()
{
int one, int two,
int sum;
cout<<"Please enter the first number"; //Requests for first integer
cin>>one; //accepts first integer
cout<<"Please enter the second number"; //Requests for second integer
cin>>two; //accepts second integer.
sum = one + two; // one & two get added and the value gets assigned to sum.
cout<<"The sum:"<<sum; //sum of 2 int's is displayed
If you like this code please
give me a good message. If you have gained some
knowledge from this tutorial please make me happy, by giving
me an “excellent rating”. That's all I ask.
10/25/2000 10:50:33 PM:Kuvrio It would be grate to have your 3
tutorials on a ZIP file to download
once.
10/26/2000 4:37:42 PM:Leprechaun I think this is a good idea, I wish I
had found something like this a year
ago.
But, me being a smart aleck, I
have to say that you should have a \n
at the beginning of your ouput, and a
space at the end like:
cout <<
10/27/2000 1:12:16 PM:Amin Patel Ok, from next time I shall start.
Thanks for the comments ( Zip and
/n+space)
10/29/2000 11:31:16 AM:Treker Today's programming standards in C++
using IOSTREAM are that you use an
endl, not a \n to mark a
newline.<BR>
cout << "Blah..." <<
endl; would be the ideal format.
10/31/2000 3:46:51 AM:R 2 k I really love these tutorials they
make me love c++ now.
11/4/2000 9:04:02 PM:mud its easier if while using cout <<endl
<<...; you also incorporate the \n
command where convenient. (i.e.: cout
<Keep the Planet clean! If this comment was disrespectful, please report it: Reason:
11/4/2000 9:06:48 PM:mud however, it can be confusing to use
both so itd be easier to learn if you
just you endl (\n versus '\n' can be
confusing)
11/6/2000 6:18:48 AM:Daniel Krusky These Tutorials are really good. Do you
have any knowledge of NetBIOS ?
12/30/2000 3:32:22 AM:mike14mon THIS IS GREAT!!! thanks im new at c++
very new ive spent long aggervating
days reading c++ in 21 tuts, im looking
foward to more articals from you
...thanks again
4/4/2001 10:57:50 AM:Smiley hey i love these tutorials but im
having a problem. when i run these once
it has ended it closes out really fast
and in this one i cant see the ending
number and in the first tutorial i
couldn't see anything cause it closed
out so fast. how can i make it stay
open?
4/21/2001 11:49:18 PM:Ryan Don't worry, that happened to me when I
first began learning. Here, try
this.
<-- start code -->
#include
<iostream.h>
#include <conio.h>
int
main()
{
//print to screen
cout
<<
5/10/2001 4:27:43 PM:Bhavendra Hi Amin,
Thanks very much and
putting ur effort on C++ and sharing ur
knowledge.
It's very helful for me I
don't know anything abt c++ now I'm
understand how to write programm in
C++. If u have any goodu documents for
beginners please let me know. thanks
onece again.
Wihs u all the best.
5/24/2001 12:22:08 AM:FTWJFIA About the program closing realy fast
when run - I had this problem too. I
did some research and found a solution
add: #include <conio.h> and at the end
of your program add: system(
5/24/2001 12:23:42 AM:FTWJFIA Sorry. At the end of your program add:
system("PAUSE"); Happy Coding!!
11/11/2001 3:13:28 PM:imn0thing with your three tutorials combined, it
makes the worst introduction to the
language i have ever seen. it is not
even good coding practice to declare
multiple variables on a single line.
but if you do, it should be like
this.
int one, two,
sum;
not
int one, int two, int
sum;
11/16/2001 2:22:44 PM:vorticon well simple and easy ot understand but
there should be more explanation in the
3rd tutorial. now a VB proggrammer is a
begginner C++ proggrammer thx
11/16/2001 4:02:50 PM:MerDeNoms Also If you want to use any of the
11/16/2001 4:04:03 PM:MerDeNoms If you want to use any of the "system"
commands, you will need to add the
directory "#include<stdlib.h>" That
will allow you to use the
commands:
system("pause"); <---Pause
the program untill you hit
enter.
system("cls"); <---Clear Screen
wich is great for cleaning up screens
on menu driven programs.
12/2/2001 5:57:30 AM:ji thanks amin. great work! and to the
intellectually impared 'imn0thing'...
once again, i think that i can speak
for most of the beginners read mr.
patel's works... you're absolutely
right... 'youAREn0thing'.
2/21/2002 7:51:54 AM:Deepsmeg II must say that I agrree w/ imn0thing.
I bought sams teach yourself C++ in 24
hours, and the first 3 chapters have
given me a far clearer understanding
than these tutorials.
4/18/2002 11:41:09 PM:zenthor i don't see why you guys are compareing
a free tutorial to a book you payed for
also whne i compile these tutorial with
vc++6 i get a debug error:
Loaded
'ntdll.dll', no matching symbolic
information found.
Loaded
'C:\WINDOWS\system32\kernel32.dll', no
matching symbolic information
found.
The thread 0x5B0 has exited
with code 0 (0x0).
The program
'C:\Documents and
Settings\zenthor1\Desktop\ctut101\Debug\
helloworld.exe' has exited with code 0
(0x0).
how do i fix this?
7/24/2002 4:23:48 AM:Whittal I love these tutorials. keep 'em
coming. They are exacly what i was
looking for. Nice and easy to
understand and you explain everything.
8/11/2002 8:16:50 PM:Jaimin Patel How long does it take you to learn C++
if you go by the tut's?
Add Your Feedback!
Note:Not only will your feedback be posted, but an email will be sent to the code's author in your name.
NOTICE: The author of this article has been kind enough to share it with you. If you have a criticism, please state it politely or it will be deleted.
For feedback not related to this particular article, please click here.