Monday 3 December 2018

what is gets function in c++

what is gets function in c++

gets() function


 function
gets function

gets() is a standard library function defined in stdio.h header file which is used to
get a string from the keyboard.Its general syntax is:

gets(string variable name);


It reads characters from stdin and stores them as a string into ‘string variable’ until
a newline character of the EOF is reached.Consider the following exemple:

/*gets example*/

#include<stdio.h>
#include<conio.h>
#include<iostream.h>

Int main()
{
char TestStr[150];

cout <<”insert your full address:”;

gets(TestStr);

cout<<”\nYour address is :”<<TestStr;

getch();

return 0;
} 

Output of the Program

Insert your full address: H.No. 12 lalazar colony kohat

Your address is: H.No. 12 lalazar colony kohat 

Sunday 2 December 2018

C++ calculater for beginner

C++ calculater for beginner

calculater for c++ beginner





#include<iostream>
#include<cmath>

using namespace std;

int main()
{
    double num1,num2;
    char operation,redo;
    cout<<"Welcome to the C++ calculater"<<endl;
    cout<<endl<<endl;
   
    do
    {
        //receiving the variables from input
        cout<<" Please enter an operation which you like to calculate (+,-,*,/,s)";
        cout<<"{s stands for swap}:";
        cin>>operation ;
        cout<<endl<<endl;
        cout<<" Please enter two numbers to apply your requested operation(";
        cout<<operation<<"):"<<endl<<"first num:";
        cin>>num1;
        cout<<"second num:" ;
        cin>>num2;
        cout<<endl;
       
        switch (operation)
        {
        case'+':
            cout<<"Addition of two numbers ("<<num1<<","<<num2<<"):";
            cout<<num1+num2<<endl;
            break;
        case'-':
            cout<<"Substraction of two numbers ("<<num1<<","<<num2<<"):";
            cout<<num1-num2<<endl;
            break;
        case'*':
            cout<<"Multiplication of two numbers ("<<num1<<","<<num2<<"):";
            cout<<num1*num2<<endl;
            break;
        case'/':
            cout<<"Division of two numbers ("<<num1<<","<<num2<<"):";
            if(num2==0)
            {
                cout<<"Not Exist"<<endl;
            }
            cout<<(num1/num2)<<endl;
            break;
        case's':
            cout<<"The swap of two numbers ("<<num1<<","<<num2<<"):";
            swap(num1,num2);
            cout<<"1stnumber="<<num1<<"and 2nd number="<<num2<<endl<<endl;
            break;
        default:
            cout<<"unknown command"<<endl;

        }
        cout<<"enter y or Y to continue:";
        cin>>redo;
        cout<<endl<<endl;
    }
    while(redo=='y'||redo=='Y');

    return 0;

}

Saturday 1 December 2018

Commets in C++ program

Commets in C++ program
Comments and their syntax in C++

Each C++ statement is terminated by a symbol named semicolon(;) which is called statement terminator.In C++, the separation between statement is specified with this ending semicolon (;) at the end of each statement. It does not matter to write more  than one statement on a single line but matter if you do not separate them with  semicolons. The use of each statement on a separate line is only to add clarity in 
the program.

Single line comment
It is represented by the double slash symbol //.It discards everything from where
The pair of slashes signs // is found up to the end of the same line.

Multiline comment

These types of comments are represented by the symbol /*...............*/. It ignores 
everything between the /* characters and the first appearance of the */ characters
, with the possibility of including more than one line.

Consider the following program to demonstrate comments.

/* program to demonstrate comments in C++ */
#include <iostream.h>
#include <conio.h>
int main()
{
cout<<”Welcome!;    //prints Welcome!
cout<< “I’m a C++ program demonstrating comments”;
getch ();
return 0;
}

Output of the program
Welcome!
I’m a C++ program

In the above example,the first lines are multi-line comments while the comment at
line  6 is a single line comment.

Friday 30 November 2018

function and statements of c++

function and statements of c++
Main() Function


The main function is the point by where all C++ programs start their execution.It
does not matter whether there are other functions with other names defined 
before
or after it, the instructions contained within this function’s definition will always 
be the first ones to be executed in any C++ program.For that same reason, it is 
essential
that all C++ programs have a main function

Body of the main() function

After the parentheses of the main function,the body of main function starts which
is enclosed in braces{ }.When this function is executed, the instructions written 
within it perform their task. Inside the body of main function, C++ statements are
written.

C++ Statements

A C++ statement is a simple or compound expression that can actually produce
some effect.It is actually an instruction to the computer for taking an action.
For example cout << “Hello world!”; is a C++ statement. The following are some
examples of statements.

cout<< “Hello World!;
In fact,this statement performs the only action of displaying the result 
“Hello World!” on the screen.

getch();
This is a standard library function defined in the header file <conio.h> and is used
to wait for the user to input a character from the keyboard.

return 0(zero);
The return statement causes the main function to return control to the operating 
system.Return may be followed by a return code.The code 0 returned by the
 teturn statement tells the operating system that the program terminates normally.


The program has been structured in different lines in order to be more readable, but in C++ ,we do not have strict rules on how to separate instructions in different lines. For example,the following program can also be written in a single line. 

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

Its single line version will be:

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

Thursday 29 November 2018

preprocessor directive in c++

Preprocessor directives (#include, #define)


A preprocessor is a collection of special statements which are executed before the compilation process.

Almost every C++ program contains a preprocessor directive.The #include 
preprocessor directives is commonly used to insert header file with extension ‘.h’.
These header files are already stored in computer in include directory. In the above program, two #include directives have been used,   #include<iostream.h>  and  
#include<conio.h>
#include<iostream.h> is used for the C++ object and #include<conio.h> for the built-in function getch().

In C++, the other commonly used preprocessor directive is #define which is used
 to define symbolic constant.Its general format is:

#define identifier value

For example:
#define PI 3.14159
#define NEWLINE ‘\n’

This define two new constant: PI and NEWLINE. Once they are defined,they can be used
in the rest of the program as if they were any other regular constant, for example:

#include<iostream.h>
#include<conio.h>
#define PI 3.14159
#define NEWLINE ‘\n’

int main()
{
double r = 5.0; //radius
double circle;
circle = 2 * PI * r;
cout <<”Area of the Circle :”<<circle;
cout<<NEWLINE;
getch();
return 0;
}

Output of the program
Area of the Circle : 31.4159

what is the structure of a c++ program

what is the structure of a c++ program

Structure of a C++ program



General syntax of a C++ program is given below.

Preprocessor directives <header file>

int main()
{
Body of the program
}

Each C++ program has three main components. These are:Preprocessor directives,
Main function and body of the main function.Now,consider the following “Hello World”
Example to explain these components.

#include<iostream.h>
#include<conio.h>
int main()
{
cout<<”Hello World!”;

getch();

return 0;
}

Output of the program

Hello World!

The result of the above program is that it print “Hello World!” on the screen.It is one of
The simplest program that can be written in C++, but it contains the fundamental
Components of almost every C++ program.

Wednesday 28 November 2018

what is header files and reserved words

a.Header files

Header files also known as include files, are standard library files that have  an extension of (.h) and which are used to hold declaration for other files.

Consider the following program:
// header file example
#include<iostream.h>
Int main()
{
cout<<”Hello,world!”<<endl;

return 0;
}
Output of the program
Hello,world!

This program prints the string “Hello, world” to the screen using cout. However, this program nev-er defines cout, so how does the compiler know about the object cout? The answer for this is that cout has been declared in a header file called “iostream” .  When the line # include <iostream.h> is used in the program, 
the compiler locates and read all the declarations from a header file named 
iostream”.
b.Reserved words
Reserved words or keywords are those words which have their special 
meaning within the C++ language and are reserved for some specific purpose .C++ reserved words cannot be used for any other purpose in a C++ program and even cannot  be used as variables.Here is a list of C++ keywords shown in Table.