User Guides Tutorial: Learn All The Basics Of C++ Language


C++ Coding??? Dread !!!!
Forget it now here is the
Complete Antidote!!!​

This is a basic guide which I have come up with for the people who are looking to go for C++. I have tried to keep it simple so that everyone can understand it.
I had been working on this for last couple of weeks. Any comments/suggestions are welcome.

NOTE: This is a beginners guide so don’t expect too much, I made this guide . Keeping Mind that learner is a newbie in C++. Hey now don’t think I am an expert. Sorry for the mistakes I have tried to cover every aspect.


INTRODUCTION​

C++ is an object oriented programming language. It was developed by Dr.Bjarne Stroustrup in the year 1983 at AT&T Bell laboratories, New Jersey {U.S.A}. It was originally named as C with classes. The name is derived with the increment operation in "C" which is ‘++’. So it is the incremented version of C.
An object oriented programming language means collection of objects which are self contain collection of both data structure and functions that interact with other objects. It adds classes, inheritance, functions overloading and operator overloading.

“With the help of C++ we can develop editors, compilers, communication systems, database and other real life time applications.

Note: - C++ codes are case sensitive so every command/codes written should be in Small Letters
[BREAK=C++ Charactor Set]
CHARACHTER SETS

There are 2 types of character sets:-
• Source Character :- The source text is created with the help of source character such as :

Alphabet: - A-Z, a-z and _.
• Digits: - 0-9.
• Special Characters: - +, -, *, /, ^, ~, %, =, !, &, |, ( ), [ ], ?, “ “, ; , : , \, #, .
ESCAPE SEQUENCE​

The characters are interpreted at run time .These escape sequence character are represented by a back slash ‘/’ followed by a character.
For e.g.:- \n, \t, \a etc.

KEY WORDS OR RESERVED WORDS

These words are reserved to do specific task and must not be used as a normal identifier name.
For e.g.:- if, for, else, do, while etc.
IDENTIFIER

Identifier are the fundamental building blocks of a program and are used to gives names to variable, function, arrays, objects, classes etc.

[BREAK=C++ Charactor Set - Constants]

CONSTANT


These are the items which cannot be changed during the program execution. Every constant used in a program as a type that is determined by its form and a value .

They are mainly of 3 types:-
• Numeric Constant
• String Constant /Literals
• Character Constant
• Floating Constant

INTEGER CONSTANT

One whole number without any factorial part. The integer constant may be Decimal (base 10), Octal (base 8), and Hexadecimal (base 16).

CHRACTER CONSTANT

A character constant in C++ can be any of the valid in ASCII character and is enclosed in single quotes (‘‘)
For e.g.:- char grade=’A’

General Character can be represented by above method but for special character like tab, backspace etc. C++ provides escape sequence as already told by me. It is represented by backslash (\) followed by one or more character.
Escape sequence non graphic characters:-

\a = Audible Bell
\b = Backspace
\f = Formfeeds
\n = New line (\n is same as endl \n is used inside quotes but endl doesn’t)
\r = Carriage Return
\t = Horizontal Tab
\v = Vertical Tab
\\ = Backslash
\’ = Single Quote
\â€= Double Quote
\? = Question Mark
\On = Octal number
\xHN = Hexadecimal Number

NOTE: -- These Escape sequence should always be written inside ““double quotes.
As “\nâ€.

FLOATING CONSTANT​

These are also called real constants, they contain factorial part too. A real constant in exponent form has two parts: a mantissa and an exponent.

SEPARATORS

( ) = Parentheses
These are used for function call and function parameter.
{ } = Braces
These are used for blocking codes having simple or compound statement.
[ ] = Brackets
These are used for enclosing subscripts in case of a single and multi dimensional arrays.
, = Comma
These are used for separating arguments list in a function.
; = Semicolon
These are used as a statement terminator.
: = Colon

These are used in case of a labeled statement.

AIRTHMATICAL OPERATORS

Some of the arithmetical operator are +,-, *, /, %.
+ = Add
Gives the addition of two or more numbers. (A+B)
- = Subtraction
Gives the subtract of two or more number (A-B)
* = Multiplication
Gives the multiple of two or more numbers (A*B)
/ = Divide
Gives the divide of two or more numbers (A/B)
% = Remainder=Modulus
Gives the remainder of two or more number (A%B)
Note: Where A & B are integers.
KEY WORDS

Keywords are reserved words in C++. They convey special message to language compiler. It has some of the following Keyword but we will learn some of them here.
asm, continue, float, new, signed, try, auto, default, for, operator, sizeof, typed, break,
delete, friend, private, static, union, do, goto, protected, struct, unsigned, while, do while,

DATA TYPES

They are used to indicate char, int, float, double, long double etc .The chart showing the allocation is as follows:-

chartfw8.png


The following are the meaning of the words written in table:-
# Char(For Character)
# Int (For Integer)
# Double (For Integer)
# Long (For Integer)
# Long Double (For Integer)
# Float (Decimals)
# Void (For non returning function)

RELATIONAL OPERATOR


<= Less than
>= Greater than
= - Equal to (Assignment Operator)
== - Double Equal to (Conditional Operator)

LOGICAL OPERATORS

&& - To give and logic
! - Not equals to
|| - To give or logic

CIN COUT

The Identifier cout>> is a predefine object that corresponds to the standard output stream.
The Identifier cin>> is a predefine object that corresponds to the standard input stream.
“<<†– Output of data.
The output of data is known as insertion output to operator.
“>>†– Input of data.

Input of data is known as extraction or to get data from the operator.
Cout<< is the command used to print the value visually in the screen and cin>> for getting the info. from user.

[BREAK=C++ Header Files]

HEADER FILES
Header files as the name suggest are the head of the program and are written outside the main body. These files contain commands to run codes like cin>>, cout<<, getch (); clrscr (); etc...
The files we are going to learn here are.

#include<iostream.h> : The main header file has the commands to run commands like clrscr(); and getch();
#include<conio.h> : This header file has codes to run cin>> & cout<<.
#include<stdio.h> : This header file has a function called gets which is used as same as cin>> but it is mainly used for entering character and it reads spaces bt. Character while cin>> doesn’t read spaces in character .

For E.g.:- cin>>name;

Suppose user had entered Tech Enclave
Then cout<<name; will give the output Tech
While gets(name); will give same output as the name entered.
Also clrscr(); means Clear the screen if this does not given then the output of previously opened will also come along the output of your current program output.
getch(); is the command to give a pause to the console output screen , if not entered then the output will be shown but if u will enter any value the screen will return to the coding window.
Ahhhhhhhh…. Now I have completed the theory part Hope every one understood it if not ask without any hesitation. Let’s start the main Programming part.

[BREAK=C++ - Starting Basic Programming]
STARTING PROGRAMMING BASICS

First while writing the program we should keep in minds that we should do less mistakes. So without wasting time I am giving the way how to write a program.
The following steps should be taken:-

#include <Header File>
void main()
{
<Variable Declaration>
<Program Statement>
getch();
}

Note: - After void main() ';' semicolon should not be placed other wise error will occur. Instead of void main() only main() can be used but at the end the program should return a value. Otherwise use void main() if u are a beginner.

Note:- Giving getch() is not an essential part of the program. It is only given to pause(rather waits for a keystroke ... more precisely your program is waiting for an input) after the completion of the program, since most people run the program pressing Ctrl+F9 and without it, it may seem to you that there was no output given.

[BREAK=C++ - Simple Programming examples]

SIMPLE PROGRAMMING​

I will start with the basic programming, so to create a simple program use the method stated above or just see the following program or download the file attached to clear our confusion.

I am starting with simple addition program:-

Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
a=10;
b=20;
c=a+b;
cout<<"Total=\t"<<c;
getch();
}

In the following program I have taken 3 variables and marked them as in Integers so they could hold Integral Values. Then I have assigned the value a=10 and b =20. U will notice that I had not assigned any value for C because c will hold the additive value of A and B. Then at the last I have printed the value.In the same way u could make pro. Of sub, div, multi and of remainder.
The above program has predefined value but if u want to enter the value while running the program foll. Steps should be taken.

Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<"Enter the value of a \n";
cin>>a;
cout<<â€Enter the value of b\nâ€;
cin>>b;
c=a+b;
cout<<"Total=\t"<<c;
getch();
}

Here user is entering the values so the total will come as the value is entered.
Remember there should not be any change in the value u have taken .

For eg. U have taken the following codes;
I am starting directly from void main()

Code:
Void main()
{
clrscr();
int a;
cout<<â€Enter the value of Aâ€<<endl;
cin>>A;

In this case program has error as u have taken small a as int and entering the value of A in cin>>. So take same variable u have taken. Hope u understand me.

Program to input character:-

Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char name[20];
cout<<â€Enter your name or any alphabet\nâ€;
cin>>name;
cout<<â€Your name/ Alphabetâ€<<name;
getch();
}

Here u will notice that after char name I have taken some value inside [ ]. I have done this because in C++ char has 1 value assigned to it but if u will enter name then C++ will print only the 1’st alphabet of the name/string. To avoid this we can define value to char variable till 128.
The main defects of the program is that it will not read spaces bt. Name, but if u want that computer should read spaces then following program should be written.

Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char name[20];
cout<<â€Enter your name or any alphabet\nâ€;
gets(name);
cout<<â€Your name/ Alphabetâ€<<name;
getch();
}

Here I had taken one extra header file and instead of cin>> I had taken gets as I had already stated earlier that cin>> does not read spaces. So gets should be entered. Hope it is clear again I am telling u any doubt ask me I will clear it.
Now some basic programs of taking out area ,simple interest etc..

To calculate the area of square.

Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int side,area;
cout<<â€Enter the side of the square so as to find the area\nâ€;
cin>>side;
area=side*side;
cout<<â€The area of the square is \tâ€<<area;
getch();
}

Here user is entering data and hence the area is find. Similarly I am showing u how to find the area but don’t expect much I written 10+ page and I had to complete till looping. So take a quick look.

Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int radius;
cout<<â€Enter the radius of the circle so as to find the area\nâ€;
cin>>radius;
area=(22*r*r)/7;
cout<<â€The area of the Circle is \tâ€<<area;
getch();
}

Here user is entering the value and hence area is find. Similarly u could find area of diff. things. If u can recognize the formula.
Now I am telling u the program of swapping of 2 variable by using 3’rd variable .

Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
//Swapping of 2 variable without using third variable
int a,b,c;
cout<<"Enter the value of variable A\n";
cin>>a;
cout<<"Enter the value of variable B\n";
cin>>b;
cout<<"The Entered value are"<<endl;
cout<<"A="<<a; 
cout<<"\t B ="<<b;
c=a;
a=b;
b=c;
//Values after swapping
cout<<"\n The Swapped value are\n";
cout<<"A="<<a; 
cout<<"\tB="<<b;
getch();
}

Here with the help of C variable I swapped the value of A and B. Always remember the transfer of value goes to right to left. As a value being transferred to c then a is empty, again b’s value is transferring to a now b is empty. Now again c value is transferred to b. So values are interchanged means swapped.
Similarly U could enter name, integer etc and can calculate diff. things but for now I am tired will post the rest soon

[BREAK=DECISION MAKING/CONDITION CODING]
DECISION MAKING/CONDITION CODING

C++ has a huge list of decision making/condition coding statement some of them that we are going to learn are:-

1)if statement
2)if else statement
3)do while statement[will learn while looping]



IF STATEMENT​

The if statement may be implemented in different forms depending on the complexity of conditions to be tested.

• Simple if statement
• If ………else statement
• If ………else ….. if ladder statement.
Note: - I have attached 2 MS word file i.e. meant to be downloaded. The 1’st one consist program related to simple programming and the other of if else programming.
You just need to download them and write the program in your compiler and enjoy.

SIMPLE IF STATEMENT

The general form of simple if else statement as already written above is:-

if(Test Condition)
{
<Program Statement>
}
statement;

For e.g.:-

Code:
if(income>4000)
{
tax = 0.1*income;
income = income – tax;
}
cout<<â€Taxâ€<<tax;
cout<<â€Incomeâ€<<income;
……

The above part of the program is testing whether the income is greater than 4000 or not. If ‘yes’ the tax @of 10% is deducted from the income and then the final v alue should be printed.

Note:-In the whole programming condition = = must be used because we are comparing/conditioning the values so always use = =. Also && should be use for and. AND || this should be used for or condition.

THE IF….ELSE STATEMENT

The if ….else statement is an extension of simple if statement. The general form is:-

if(Test Condition)
{
<Program Statement>
}
else
{
<Program Statement>
}

If the test condition is true then the program statement under if braces will be executed. In either case if the condition become false then else statement will proceed.

Note: - You need to put braces { } at the starting and at the end if to or more program condition is given otherwise it may cause error. For e.g.:-

For e.g.:-

Code:
			If (code<0)
			Cout<<â€Code is negative numberâ€;
			
			else
			
			Cout<<â€Code is positive numberâ€;


In above program, if code is less than 0 than the code number entered is negative if not then it is positive.


SOME PROGRAMING BASICS​

Code:
#include<iostream.h>
#include<conio.h>

void main()
{
	clrscr();
	int a,b;

	cout<<"Enter the 2 no";
	cout<<endl<<"Enter A\n";
	cin>>a;
	cout<<"Enter B";
	cout<<"\n";cin>>b;

	if(a>b)
	cout<<"A is Greatest\n";
	else
	cout<<"B is Greatest \n";

	getch();
	}

This program is absolutely correct but if the same program is written with few more condition then following steps should be taken:-

Code:
#include<iostream.h>
#include<conio.h>

void main()
{
	clrscr();
	int a,b;

	cout<<"Enter the 2 no";
	cout<<endl<<"Enter A\n";
	cin>>a;
	cout<<"Enter B";
	cout<<"\n";cin>>b;

	if(a>b)
	{
	cout<<â€A=\tâ€<<a;
	cout<<â€B=\tâ€<<b;
	cout<<"A is Greatest\n";
	}
	 else if(a==b)
	{
	cout<<â€A=\tâ€<<a;
	cout<<â€B=\tâ€<<b;
	cout<<"Both are equal\n";
	}
	else
	{
	cout<<â€A=\tâ€<<a;
	cout<<â€B=\tâ€<<b;
	cout<<"B is Greatest \n";
	cout<<â€A=\tâ€<<a;
	cout<<â€B=\tâ€<<b;
	}

	getch();
	}

Here = = is used because comparison is done between 2 or more variable if u want to put character then use single quotes ( ‘ ‘ ) or double quotes. But unfortunately we cannot use more than one alphabet also if u assigned the value of char to 128. The e.g. given here is directly started with if else so don’t get confused:-

For e.g:-

if(day= =’s’&& day= =’S’)

cout<<â€The day is Sundayâ€;

But if u will give more than one alphabet then error will come. In the under written program.

if(day= =’su’ && day= =’Su’)

cout<<â€The day is Sundayâ€;

THE IF…..ELSE….IF LADDER

This is another way of putting it together when multiple decision are involved. A multiple decision is a chain of its in which several statements are attached with each if and else conditions.
The general form Is:-

if(condition a)
{
<Program Statement>
}
else if<condition b>
{
<Program Statement>
}
else if<condition c>
{
<Program Statement>
}
………

This construct is known as if….else…..if ladder . The condition are evaluated from top to down. As soon as a true condition is found the program statement attached to it will be executed otherwise compiler will look for other condition .
This type of program can be used for making results as follows.:-
The program written below shows Simple Interest based on several condition.

Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
	int t;
	double amt,si;

	cout<<"Enter the value of amount\n";
	cin>>amt;
	cout<<"Enter the total time period\n";
	cin>>t;

	if(amt>50000 && t>5)
	si=(amt*t*11)/100;

	else if(amt>=20000 && amt<50000 && t>8)
	si=(amt*t*12/100);

	else if(amt<20000 && t>=1)
	si=(amt*t*6)/100;

	cout<<"\nThe amount entered=\t\t"<<amt;
	cout<<"\nThe Time entered=\t\t"<<t;
	cout<<"\nThe Int. charged=\t\t"<<si;
	cout<<"\nThe total amt. to be paid=\t"<<amt+si;

	getch();
	}

[BREAK=ADDITION AND SUBSTRACTION IN IF ELSE]
ADDITION AND SUBSTRACTION IN IF ELSE

As the topic state it is very clear for one to understand that how to add and sub. Using if else. Now most of u will be thinking that this can be done buy simple programming then why to use if else. Ya your thinking is absolutely correct we don’t have to use if else while adding but imagine u are subtracting using simple programming and a user entered values as follows:-

You’re coding for Subtraction using simple programming:-

C= (a-B);

Suppose user entered a=10 and b= 8 then answer will come 2. But if user entered a=8 b=10 then answer will come -2. So to come out from this -2 mistake[Actually it is not a mistake but we have to get positive value] we use if else so the coding goes as follows:-

Code:
#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int a,b,c;

cout<<â€Enter the value of a and b\nâ€;
cin>>a>>b;

if(a>b)
c=(a-b);
else
c=(b-a);

cout<<â€The Subtract of A and B is\tâ€<<c;
getch();
}

So that’s the end of if else hope u understand it plz guys if u have any confusion just ask, I am here to solve your problem .
Remind u again download the attached Program files.

[BREAK=POST & PRE OPERATOR]

POST & PRE OPERATOR​

An operator that require only one operand or data item are called unary operators. C++ supports unary minus(-), ++ (Increment), -- (Decrement) on airthmatic operands.
In C++ the other unary operator are ++ (Increment), -- (Decrement). These operator can be used before or after the variable, to give condition type increment and decerement.
INCREMENT OPERATOR
++ Increases the value by one. If it is used before or after the variable.....

Code:
i++ means i=i+1;
++i means i+1=i;

Both the formula are very diff and can the whole output, i=i+1 means that incrementation is done after the value is called. But i+1=i; means incrementation is done before the value is called.
For e.g. :-

Code:
[B]int i;
i=10;
i=i++;
cout<<i;[/B]

Here first the value is incremented then the value is printed and the output will come 11. But if the same program is written in a way like this:-

Code:
int i;
i=10;
cout<<i++;

Then the output will be 10 no incrementation will be done beacuse as I already told i++ means i=i+1
so the compiler will first print the value of i then it will increament.

Also the same program with diff result:--
Code:
int i;
i=10;
cout<<++i;

Here the output will be 11 incrementation will be done beacuse as I already told ++i means i+1=i
so the compiler will first increament the value then it will print the value of i.
Note:- The following topic is important if U have any problem then please ask...

For e.g: -
Code:
[B]int i,j;
i=10;
j=i++;
cout<<i<<" "<<j;[/B]

Here the output will be 10 11 . Here the value of i is assigned to j then in the value of i there is post incrementation of 1 so the final value of i remains same and j is 11.

MULTIPlE INCREMENTS​

SO after simple increment and decrement move into the second chamber of complicated increments this type of increment are rarely used in programs but they can be asked in the outputs so as a guider I have to tell everything .
These type of increments and decrements are used in same line and with same integar or diff integar..

For e.g. :-----
Code:
[B]void main()
{
clrscr();
int x;
x=10;
cout<<x++<<" "<<++x;
getch();
}[/B]

In the above program there are 2 output in the same line and the integer is same so the output will go from left to right but the assigning of numbers will be from right to left..

As the initial value of x is 10 and for assigning heading from right the first thing is ++x means x+1=x so the value become 11. Then there is x++ means x=x+1; so the comp will 1'st print the value then it will increment it so the value remains same as 11.The final output is 11 11.

I am not going into deep of this because this is not too much important as far as I know. SO lets start the loop.


[BREAK=LOOPS /REPETITIONS/ITERATIONS]
LOOPS /REPETITIONS/ITERATIONS​

A loop can be defined as the repition of the statements until the condition got fulfilled.Looping is consisted of statements that are executed until some condition for termination of a loop.

A program loop normally cosists of 2 segment:-
  • Control Statement
    [*]Body of the loop
The control statement test the condition and then direct the repeted execution of the statement contained in the body of the loop.
The body of the loop consists of the program statement that are given by the programmer....

The looping process in general performs the following 4 steps:---
  • Setting and Initialization of the counter.
    [*]Execution of the statement in the loop.
    [*]Checking of the condition to run the loop for specific period of time.
    [*]Incrementing the counter.

The C++ provides three loop constructs for performing loop operations. They are:

  • [*]The while statement.
    [*]The do while statement.
    [*]The for statement.

[BREAK=THE WHILE STATEMENT]
THE WHILE STATEMENT

The simplest of all the loop :D. This looping structure is known as while loop, the basic format of while loop in C++ language....
Code:
while(test condition)
{
//Body of the loop
program statement
}
The following points should be remembered while using while loop :---
  1. (1)The loop will not get executed if the given condition becomes false.
  2. (1)The loop will be executed till the condition is false the result will be presented as soon as the condition becomes false.
  3. There must be some looping termination inside the loop to avoid abnormal output..
Here are some e.g. To clear ur doubts........
Note:-- Plz download the attached documents to know better programming through while loop and all :D
  • Program to show no. From 1-100 using while loop..
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
i=1;
while(1<=100)
{
cout<<i<<",";
i++;
}
getch();
}

Here the condition is that the loop should run till the value of i becomes 100 as soon as it becomes 100 the loop ends and the output will be shown.
Firstly the value is 1 then the condition is fulfiled that 1 is less than 100 prog. Will go inside the loop the 1'st statement is cout<<i the initial value of i is 1 so 1 will be the 1'st output then acc/que[/B] the second value should be 2 then 3 and so on upto 100 so we have to write i++ so there will be an increment of 1 in the value of i and the loop goes upto 100......:)
  • Program to add value upto 100..
I am directly starting from void main() so dont get confused ....:D

Code:
void main()
{
clrscr();
int i,s;
i=1;
s=0;
while(i<=100)
{
s=s+i;
i++;
}
cout<<"Sum =" <<" "<<s;
getch();
}

Here I had taken 2 variables i for taking the values from 1-100 and sum for adding those values... I had asssigned the value of s=0 because 0 is an additive properties of add.. If u will add some no.. to 0 the no. Remains same ....Similarly 1 is the multiplicative prop. Of Multi..:D:D . Here first the value of i is 1 then we add the value into s. The initila value of s is 0 so after add.. the value become 1 then i got incremented again loop will run and again there will be addition. And the vaue of s was 1 after adding 2 it becomes 3 ad the loop goes on till the value of i becomes 100...

  • To find out the factorial of any no..
Code:
void main()
{
int n,f;
f=1;
cout<<"Enter the value of i to find the factorial\n";
cin>>n;
while(n!=0) or (n>0)or (n>=1)
{
f=f*i;
i--;
}
cout<<"Factorial ="<<" " <<f;
getch();
}

Here the user will in enter the no.. and we have to find the factorial. For ur info.. telling as most of u had forgotten what is factorial, the number resulting from multiplying a whole number by every whole number between itself and 1 inclusive.

For e.g. :- Fact. Of 5 :--- 5*4*3*2*1= 120. :D
So to find the factotorial the prdecessor no. Includin the no should be mulitiplied... To find this i had taken a variable in which the entered value will be stored. And variable f to hold fac. Value.. First I had given the value of f 1 as i had told 1 is the multiplicative prop. Of Multi..:D:D.. SO multiplying an no with 1 will give same no.. Suppose user enters th value 5 first the condition will be checked. I had given 3 cond. All 3 are correct but u had to use any one of em.. So prog. Will enter the loop then the no will be multi . With f and the value of n will get decremented again it will get multi. Till the condition becomes false . And finally the output wil be given

[BREAK=DO WHILE STATEMENT]

DO WHILE STATEMENT​

The do while loop is the 2'nd type of loop, the main positive aspect of thios loop that whether the condition is true or false the loop will run one time, if the condition will be true then the loop will be continued otherwise it will stop..

The syntax of the do while statement is as follows:-

Code:
[B]do
{
<program statement>
}
while(<condition>);[/B]

Here the condition is at the last so the loop will run 1'st time till reaching the condition....

The followind point should be remembered while using do-while loop:---

  1. (1)It is executed at least once..
  2. (1)It is executed till the condition remains trueand the control comes out as soon as the loop ends.
  3. (1)There must be some looping terminating condition otherwise the loop willnot stop...
Here are some e.g. To clear ur doubts........
  • To check whether the number is Armstrong or not....
Note:- An armstrong no. Is a no. Whose digit cube sum is equal to the same no..

Code:
void main()
{
clrscr();
int n,k,r.s;
cout<<"Enter the number\n";
cin>>n;
k=n;
s=0;
do
{
r=n%10;
s=s+r*r*r;
n=n/10;
}while(n!=0);
if(k==s)
cout<<"It is Armstrong no.";
else
cout<<"It is not Armstrong No..";
getch();
}

In the above program I had taken 4 variables n for storing the number, k for storing the value of n so that we can check the condition,s for adding,r for taking remainder or storing every digit of the entered number...
So suppose u had entered the no 153 the no. Gets stored in k so n and k both will hold 153. Now the loop will begins first acc/cond given no will get divided be 10 and the remainder will get stored in r. First when u will divide 153 by 10 the remainder will come 3 the in second line cube of 3 i.e 27 will be added to s i.e 0 so s is now 27 then the remaining part will again got divided and the cube of 5 i.e 125 then again the last no cube i.e 1 will be added so the sum of 27+125+1=153 so the no. Is armstrong no..

  • To find factorial of any number.
Code:
[B]void main()
{
clrscr();
int n,f;
cout<<"Enter the number\n";
cin>>n;
f=1;
do
{
f=f*n;
n=n-1;
}while(n!=0);
getch();
}[/B]
I have already explained it so its should be clear to all...:D

[BREAK=FOR STATEMENT]

FOR STATEMENT
I dont know why but this is the only loop which i finds the best. This loop is an entry controlled loop means condition should be fulfiled to enter it.The syntax of this loop is as follows:--

Code:
for(initialisation;condition;increment/decrement)
{
<program statement>;
}
  • Initialisation Expression:-
It is executed only when the loop 1'st start. It provides loop variable an initial value.
  • Test Expression:-
It involves relation operator. It is executed every time through the loop before the body of the loop is executed . If the test expression is true the loop wil go on otherwise the loop wil end.
  • Increment/Decrement (re-initialising) expression:-
It os always executed at the end of the loop after the body of the loop.
Here are some e.g. To help u:D
  • For the following output:
*
* *
* * *
* * * *
* * * * *


Code:
void main()
{
clrscr();
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<"*"<<" ";
cout<<"\n";
}
}
getch();
}
  • For finding out the factorial of any number.
Code:
void main()
{
clrscr();
int i,f,n;
cout<<"Enter the number\n";,
cin>>n;
f=1;
for(i=n;i>=1;i++)
{
f=f*i;
}
cout<<"The factorial is =\t"<<f;
getch();
}
  • For displaying the table of any number..

Code:
void main()
{
clrscr();
int i,n,t;
cout<<"Enter the number\n";
cin>>n;
for(i=1;i<=10;i++)
{
t=i*n;
cout<<t;
cout<<"\n";
}
getch();
}

:thanx: Next Part will be upgraded soon :)
Till then enjoyyyy..:)
Regards Darky :rofl:
[/FONT]
 
Forgive me for my ignorace, but in what respect(practically) it can help me. I see its programming but how can it be helpful to me.Thanks.
 
Sorry for being pedantic, but your "tutorial" contains so many factual errors and non-standard programming practices that beginners are better off learning C++ the usual and proven way - i.e. using a proper book. I would rather suggest you to learn standard ISO C++ from ground-up than wasting time writing such tutorials.

Beginners looking for a solid C++ tutorial are encouraged to take a look at C++ Language Tutorial and C programming.com - Your Resource for C and C++ Programming.
 
hey darkey ..even u in 12th naah ??..
actually studyin phy ,chem and maths...i ve got lots of back log of Computer science .both hardware and software..specially c++ programming..
and i need to cover up before ..jan starts...

so wanted to ask is this tutorial ..somewhat according to da 12TH syllabus ??..hmmm anyways i am takin a printout of this..and will use it to study on the go..thanx a lot..

also i found another site for some nice guide and complete c++ programs..

here it is :
http://www.syvum.com/a/cpp/
 
Hi :)
hav a little doubt about the header file used for lib functions getch(),clrscr()
do these belong to <conio.h> or as you mentioned <iostream.h>?
Thnks
newboy
 
Digging this up.. I am jobless after exams.. :D nice work for beginners..

I found a silly error

cin>>radius;
area=(22*r*r)/7;
r should be radius or vice versa :D

and yeah, its better to learn standard C, but these 12th std folks and even ppl in Engineering use Turbo C! so it will take some time for ppl to start writing #include<stdio> instead of #include<stdio.h> :D
 
Surely you mean #include<cstdio>? #include<stdio> will not work, as stdio.h is a C header.... you'd need to include either cstdio or stdio.h
 
Let me explain how this stuff works.

iostream.h is a C++ header. Traditionally it was included as so "#include <iostream.h>"

Now, when the STL and C++ runtime library were standardized, there had to be a way to support both the new semantics, and the old behavior (present in iostream.h etc). So now, if you want to use the STL and the standard C++ runtime library you say "#include <iostream>"; if you explicitly want the old behavior, you say "#include <iostream.h>". The latter is deprecated, and should not be used in new programs. Note that the standard version (i.e. the non .h version) also introduces namespaces and hence the need to either say "using namespace std;" or prefix stuff like cout with std, thereby saying "std::cout<<".

stdio.h, on the other hand, is a C header. It has nothing to do with C++. You can say "#include <stdio.h>", but it is even further from the standard C++ semantics than iostream.h. So, the new header they introduced is cstdio, and you can include it as "#include <cstdio>".

There are also cstdlib, cmath and a bunch of other such headers...
 
KingKrool said:
Let me explain how this stuff works.
iostream.h is a C++ header. Traditionally it was included as so "#include <iostream.h>"
Now, when the STL and C++ runtime library were standardized, there had to be a way to support both the new semantics, and the old behavior (present in iostream.h etc). So now, if you want to use the STL and the standard C++ runtime library you say "#include <iostream>"; if you explicitly want the old behavior, you say "#include <iostream.h>". The latter is deprecated, and should not be used in new programs. Note that the standard version (i.e. the non .h version) also introduces namespaces and hence the need to either say "using namespace std;" or prefix stuff like cout with std, thereby saying "std::cout<<".
stdio.h, on the other hand, is a C header. It has nothing to do with C++. You can say "#include <stdio.h>", but it is even further from the standard C++ semantics than iostream.h. So, the new header they introduced is cstdio, and you can include it as "#include <cstdio>".
There are also cstdlib, cmath and a bunch of other such headers...

& it's always better practive to use updated (newer ) files.... & btw dev c++ is a much better compiler.. but not as good as gcc or g++..
 
i don't know abc of c++ , but can u plz explain where to write these commands, like open a text etc file and start writing or do i need any software, plz pm me, thanks
 
Back
Top