Input & Output Basic C++

Input and Output

Input and Output

A program may need certain inputs from the user for working properly. The term standard input refers to the input via keyboard. The process of getting something from computer is known as output. The output is mostly displayed on monitor. The term standard output refers to the output displayed on monitor. The result of a program is the output of that program.

Input and Output

C++ uses different streams to perform input and output operations. A stream can be defined as flow of data. It is an object that is used by a program to insert or extract characters. The standard C++ library includes the header file iostream.h. It contains the declarations of all standard input and output stream objects. The header file iostream.h must be included in a program that uses any input or output statement.( Input & Output Basic C++)

Standard Output

By default, the term standard output refers to the output displayed on the screen C++ uses the cout stream object to display standard output. The word 'cout stands for console output. The cout object is used with insertion operator (<<). The message or the value of variable followed by the insertion operator is displayed on the screen.

Syntax

The syntax of using cout object is as follows:

cout<< exp;

cout

It is the name of the object used to display standard output.

<< 

It is known as insertion operator or put to operator. It sends the output to cout object. The cout object then sends the output to the screen. The left side of << operator must be an output stream like cout. The right side of << operator must be an expression or manipulator. One statement can use multiple insertion operators to display the values of multiple variables or constants etc.

exp

It is the variable, constant or expression whose value is displayed on the screen .

 Example 1

The following example will display "Pakistan Zindabad" on the screen. The string constant is always enclosed in double quotation.

  • cout<<"Pakistan Zindabad";

Example 2

The following example will display 720 on the screen. The numeric constant is not enclosed in quotation marks.

  • cout<<720;

 Example 3

The following example will display a message along with the value of variable a. The variables are not enclosed in quotation marks. The insertion operator <<< is used twice for string and the variable.

  • a = 100;
  •  cout<<"The value of a is "<<a;

C++ Program that displays a message and values of integer and character variables:

#include <iostream.h>

#include <conio.h>

void main()

{

clrscr();

int n = 10;

char ch ='*';

cout<<"Testing output”;

cout<<n;

cout<<ch;

getch();

}

How above Program Works

The above program declares and initializes two variables n and ch. It displays message Testing output on the screen. It then displays the values of variables n and ch. The output appears on the same line. The output of second statement starts where the output of first stalement ends.

C++ Program that adds two floating point numbers and shows the sum on screen.

#include <iostream.h>

#include <conio.h>

void main()

{

clrscr();

float var1, var2, res, var1 = 24 27;

var2 = 41.50;

res = var1 + var2;

cout<<var1<<" + "<<var2<<" = "<<res;

getch();

}


C++ Program to calculate and print the area of rectangle with given height and width.

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int height, width, area;

height 5;

width= 4;

area = height * width:

cout<<"Area of rectangle="<<area;

getch()

}


Working of above Program

The first line declares three integer variables. The second and third lines assign values to the variables height and width. The next line uses an arithmetic expression to calculate the area of rectangle and stores result in variable area. The last line displays the result on screen.

Post a Comment

0 Comments