article

Tutorial 3

Email
Submitted on: 1/1/2015 1:25:00 PM
By: Amin Patel1 (from psc cd)  
Level: Beginner
User Rating: By 64 Users
Compatibility: C++ (general)
Views: 1165
 
     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.

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


 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.