C++ :- Structure of a program

A Standard C++ Program follows the below structure

1. Documentation
2.Preprocessor Statements
3.Global Declarations
4.The main() function
          a.Local Declarations
          b.Program Statements & Expressions
5.User Defined Functions

Syntax :-


#include<Header File Name>
-------------------------------
-------------------------------

return_type main()
{
    statement ---1;
    statement ---2;
    --------------
    --------------
    statement ---n;
}

Points to remember :-

Here are some points where we need to remember while developing a C++ program
1. This is advisable that C++ program must be written by using lowercase letters to avoid error at run time because C++ is a case sensitive language.
2. Each statement must be terminated by using ;(semi-colon).
3. Group of statements must be enclosed between { and }.

I/O Operators :-

1. cin :-

The purpose of cin object is to take the input from the user.

Syntax :-

cin >> variableName;

Example :-

cin >> age;
cin >> salary;

2. cout :-

The purpose of cout object is to dsisplay the information on the standard output device.

Syntax :-

cout << variableName;

Example :-

cout << age;
cout << salary;

Note :-

To perform the IO operations in a c++ program, User must include the associated header file named "iostream" and a namespace named "std" as below
#include<iostream>
using namespace std;

Cascading of I/O Operators :-

Multiple use of I/O operators in a single line is called cascading of I/O operators.

Syntax :-

cout << Item1 << Item2;
cin >> Item1 >> Item2;

Example :-

cout << "salary = " << salary;
cin >> age >> salary;

Note :-

The operator used for taking the input is known as the extraction or get from operator (>>), while the operator used for displaying the output is known as the insertion or put to operator (<<).

P1 : Write a program to accept the qty and unit price of a product from the user and display their amount.

#include<iostream>
using namespace std;
int main()
{
	int qty;
	float uprice,amt;
	cout<< "Enter the qty=";
	cin >> qty;
	cout<<"Enter the Unit Price=";
	cin>>uprice;

	amt = uprice*qty;

	cout<< "amount = "<<amt; return 1;
}

P2 : Write a program to accept the principal amount, Rate of interest and Time from the user and display Simple Interest.

#include<iostream>
using namespace std;
int main()
{
	float pa,rate,time,si;

	cout<< "Enter the Principal Amount=";
	cin >> pa;
	cout<<"Enter the Rate and Time=";
	cin>>rate>>time;

	si = (pa*rate*time)/100;

	cout<< "SI = "<<si; return 1;
}

Questions

Q1 : What cout and cin are?

Ans :- cout is object of type ostream. cin is object of type istream

About Company

TechiesEra is a quality Training Center of Noida. TechiesEra provides summer training in Noida on Hadoop, MongoDB,CouchDB,NoSql,Dot Net, Android, Java,PHP etc with live projects. TechiesEra is group of highly qualified professionals in the field of IT education & recruitments.

Get in Touch

  • Phone: +91-9821384666
    9560231801 / 9711965544
    0120 - 4292580
  • Email:
    info@TechiesEra.com
  • Address:
    C-33, Sector - 2
    Near By : Niruala Hotel
    Noida - 201301