Important alert: (current site time 7/15/2013 4:42:44 PM EDT)
 

article

Tutorial 3

Email
Submitted on: 10/18/2000 4:04:30 PM
By: Amin Patel1 
Level: Beginner
User Rating: By 64 Users
Compatibility: C++ (general)
Views: 89125
(About the author)
 
     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.
				
Tutorial 3- By: Amin Patel     About Me  
c, c++, c__,tutorial,been,made,give,introduction,programm

 Planet Source Code -                      Start with a prayer to God, that you “Succeed in learning C++"

 
 
Advanced Search

 

Previous Tutorials:
Tutorial 1 Tutorial 2


Only at PS Code

Code of the Day
To join the 'Code of the Day' Mailing List click here!

 

Thank you for coming to Planetsourcecode 
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 -->
    Now Pay attention, with a little explanation and review of the first tutorial we shall proceed. Some people requested for the theoretical details.
- 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. 

// << 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

return 0; //program has ended
}

<-- content_end-->

_______________________________________________________________________

  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.

_______________________________________________________________________

 Tutorial 3 - By: Amin Patel

 

Categories | Articles and Tutorials | Advanced Search | Recommended Reading | Upload | Newest Code | Code of the Month | Code of the Day | All Time Hall of Fame | Coding Contest | Search for a job | Post a Job | Ask a Pro Discussion Forum | Live Chat | Feedback | Customize | C/ C++ Home | Site Home | Other Sites | About the Site | Feedback | Link to the Site | Awards | Advertising

   C++ - Tutorials

Copyright© 1997 by Exhedra Solutions, Inc. All Rights Reserved.   By using this site you agree to its Terms and Conditions.   Planet Source Code (tm) and the phrase "Dream It. Code It" (tm) are trademarks of Exhedra Solutions, Inc.


Other 3 submission(s) by this author

 


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
10/22/2000 6:14:48 AMPicklezz

Can't yu give a tutorial in starting making your first app?
(If this comment was disrespectful, please report it.)

 
10/22/2000 7:43:37 AMAmin

Refer tutorial 1
(If this comment was disrespectful, please report it.)

 
10/25/2000 10:50:33 PMKuvrio

It would be grate to have your 3 tutorials on a ZIP file to download once.
(If this comment was disrespectful, please report it.)

 
10/26/2000 4:37:42 PMLeprechaun

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 <<
(If this comment was disrespectful, please report it.)

 
10/27/2000 1:12:16 PMAmin Patel

Ok, from next time I shall start. Thanks for the comments ( Zip and /n+space)

(If this comment was disrespectful, please report it.)

 
10/29/2000 11:31:16 AMTreker

Today's programming standards in C++ using IOSTREAM are that you use an endl, not a \n to mark a newline.
cout << "Blah..." << endl; would be the ideal format.
(If this comment was disrespectful, please report it.)

 
10/30/2000 12:14:03 AMAmin Patel

Ok
(If this comment was disrespectful, please report it.)

 
10/31/2000 3:46:51 AMR 2 k

I really love these tutorials they make me love c++ now.

(If this comment was disrespectful, please report it.)

 
11/4/2000 9:04:02 PMmud

its easier if while using cout <
(If this comment was disrespectful, please report it.)

 
11/4/2000 9:06:48 PMmud

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)
(If this comment was disrespectful, please report it.)

 
11/6/2000 6:18:48 AMDaniel Krusky

These Tutorials are really good. Do you have any knowledge of NetBIOS ?
(If this comment was disrespectful, please report it.)

 
12/30/2000 3:32:22 AMmike14mon

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
(If this comment was disrespectful, please report it.)

 
4/4/2001 10:57:50 AMSmiley

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?
(If this comment was disrespectful, please report it.)

 
4/21/2001 11:49:18 PMRyan

Don't worry, that happened to me when I first began learning. Here, try this.
<-- start code -->

#include
#include

int main()
{
//print to screen
cout <<
(If this comment was disrespectful, please report it.)

 
5/10/2001 4:27:43 PMBhavendra

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.
(If this comment was disrespectful, please report it.)

 
5/24/2001 12:22:08 AMFTWJFIA

About the program closing realy fast when run - I had this problem too. I did some research and found a solution add: #include and at the end of your program add: system(
(If this comment was disrespectful, please report it.)

 
5/24/2001 12:23:42 AMFTWJFIA

Sorry. At the end of your program add: system("PAUSE"); Happy Coding!!
(If this comment was disrespectful, please report it.)

 
11/11/2001 3:13:28 PMimn0thing

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;

(If this comment was disrespectful, please report it.)

 
11/16/2001 2:22:44 PMvorticon

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
(If this comment was disrespectful, please report it.)

 
11/16/2001 4:02:50 PMMerDeNoms

Also If you want to use any of the
(If this comment was disrespectful, please report it.)

 
11/16/2001 4:04:03 PMMerDeNoms

If you want to use any of the "system" commands, you will need to add the directory "#include" 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.
(If this comment was disrespectful, please report it.)

 
12/2/2001 5:57:30 AMji

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'.
(If this comment was disrespectful, please report it.)

 
2/21/2002 7:51:54 AMDeepsmeg

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.
(If this comment was disrespectful, please report it.)

 
4/18/2002 11:41:09 PMzenthor

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?
(If this comment was disrespectful, please report it.)

 
4/29/2002 5:39:23 PMCodeLearner

Excellent tutorial, thanks. :)
(If this comment was disrespectful, please report it.)

 
7/24/2002 4:23:48 AMWhittal

I love these tutorials. keep 'em coming. They are exacly what i was looking for. Nice and easy to understand and you explain everything.
(If this comment was disrespectful, please report it.)

 
8/11/2002 8:16:50 PMJaimin Patel

How long does it take you to learn C++ if you go by the tut's?
(If this comment was disrespectful, please report it.)

 
8/27/2002 6:02:13 PMdavidswinstead

You've got to be kidding me, that's really it? I could GUESS that much C++ ffs. I was hoping to actually learn something I could use to make... anything. I can't believe it actually took somebody 3 seperate tutorials just to get as far as declaring variables.
(If this comment was disrespectful, please report it.)

 
9/16/2002 4:49:01 PM

I thought it was very helpful! It clears some confusion in my mind!

(If this comment was disrespectful, please report it.)

 
10/5/2002 7:26:59 PM

u're good
(If this comment was disrespectful, please report it.)

 
11/6/2002 12:05:50 PM

I have a question what is
(If this comment was disrespectful, please report it.)

 
12/24/2002 7:05:47 AM

To Keep Open First Include File conio.h
and in the end of your code (or before return 0) put without '< >'
(If this comment was disrespectful, please report it.)

 
3/1/2003 2:20:36 PM

Thanks for writing this dude.
(If this comment was disrespectful, please report it.)

 
4/23/2003 10:39:40 PM

Thanks! Very helpful to a newbie - first day!
(If this comment was disrespectful, please report it.)

 
7/2/2003 11:54:51 AMJustin W.

It would be easier to have it like cout << "\nThe sum is" << num1 + num2, instead of declaring a sum variable.
(If this comment was disrespectful, please report it.)

 
7/9/2003 11:57:39 AMJenny S-T

This tutorial may not do very advanced stuff but is great for a complete beginner and only takes 10 minutes, not 3 hours. Thanks Amin.
(If this comment was disrespectful, please report it.)

 
8/26/2003 7:30:17 PM

thanx man your helping me grasp the knowledge that is...c++
beginner
(If this comment was disrespectful, please report it.)

 
12/14/2003 10:38:00 PMDax Spencer

to hard to understand i mean should i use standered or now standered?
(If this comment was disrespectful, please report it.)

 
12/16/2003 10:33:30 AM

Good work, but my compiler gives me warnings about the variable declarations. I agree with imnothing you should have put: int one, two, sum;

(If this comment was disrespectful, please report it.)

 
8/9/2004 10:11:21 PM

Thankyou for your intrest and time to know to newbie C++ learner , its realy good to have the free codes on net , especially the way you write is so easy to understand for newbie like me , i wish i could ever write and give out C++ stuff to newbie , i would read more tutorials from you , Thanks
(If this comment was disrespectful, please report it.)

 
9/27/2004 11:26:32 AM

Great tutorial,
could you use: sum = one * two; to x the variables together?
(If this comment was disrespectful, please report it.)

 
11/1/2004 6:47:54 PM

Thanks for breaking it down to this level. I'm just beginning to move up from years of VB. 5 big ones from me, great work!!!
(If this comment was disrespectful, please report it.)

 
11/16/2004 8:26:56 PMfatcat92

Wow, this tutorial is great. Wait a minute, no its not. Your just doing variables now? And these comments, "aww this is so great thx u", are you people CRAZY?! You'll never learn how to program using this, its going to slow, learning variables for so long, if you are going to, atleast teach them "char, long, or anything else along with int.

Your explaining to much, alot of effort, but your going to slow.
(If this comment was disrespectful, please report it.)

 
4/19/2005 2:25:09 PM

To the one having trouble keeping the program open to see it run, try running from the DOS prompt. Make sure your in the right directory, and type the name of the compiled program only. It will run and stay in the DOS screen, not go back to windows. Or in these simple programs, try - cin.get() before the return 0; line. It will make you hit the enter key before exiting.
(If this comment was disrespectful, please report it.)

 
9/15/2005 7:44:04 PMel

for starters
(If this comment was disrespectful, please report it.)

 
1/12/2006 9:37:32 PMDIlmer Valecillos

Very good Turorial Right to the point!
(If this comment was disrespectful, please report it.)

 
1/25/2006 12:29:18 AMjeffrey

wow im greatly annoyed, it seems your tutorial is write but everytime i use coding from and tutorial the dev c++ compiler tells me its wrong. can someone pls tell me why this is or send me some code which does work with dev c++ or even suggest a compiler which will let it work. thanks
(If this comment was disrespectful, please report it.)

 
11/13/2006 6:45:28 AM

Hi Amin,

Thanks very much and putting ur effort on C++ and sharing ur knowledge.

(If this comment was disrespectful, please report it.)

 
2/8/2007 12:52:26 AM☼☼☼☼

jeffrey.... try hello world program....
_________________________________________

#include
using namespace std;

int main ()
{
cout << "Hello World!";
return 0;
}
(If this comment was disrespectful, please report it.)

 
2/8/2007 12:54:10 AM☼☼☼☼

#include
using namespace std;

int main ()
{
cout << "Hello World!";
return 0;
}

(If this comment was disrespectful, please report it.)

 
12/30/2007 3:43:37 PMSean

THANKS this was a big help and was well explained!!
(If this comment was disrespectful, please report it.)

 
9/25/2008 5:31:45 AMmyab5

im very thankfull 4 u i really like the way u explain i feel as if my friend is explaining to me
(If this comment was disrespectful, please report it.)

 
9/28/2008 11:19:37 AMxylo

thx for the tutorial i had 1 problem tho i keep getting an error on the line that says
int one,int two,int sum, how do i fix it did i do sumthig wrong or is it just me
once again thx for your help
(If this comment was disrespectful, please report it.)

 
6/28/2012 8:10:09 AMxylo................

int one,two,three.;
int sum;
sir...am i right...pls tel me....
(If this comment was disrespectful, please report it.)

 
6/28/2012 8:13:53 AMxylo................

tnx for that great tut sir.....but can u whrite a tut on how to create a payroll.........??wish u could answer my request.....
(If this comment was disrespectful, please report it.)

 

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.