Input and output in c++

Download PDF
Advertisement

Input and output in C++

Input and output in c++! The process of going something to the computer is known as input. The input is mostly given by the keyboard. Programs may need certain inputs from the users for working properly. The term standard input refers to the input via the keyboard. input and output defined in the following.

The process of getting something from the computer is known as output.

Advertisement
The output is mostly displayed on the monitor. The terms standard output refers to the output display on the monitors. The result of the program is the output of that program.

C++ uses different streams to perform input and output operations. A stream can be defined as s flow of data. It is an object that is used by a program to insert or extra 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 an input or output statement.input and output defined in the following.

Standard Output

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

Syntax:

The syntax of using cout object is as follows:

  cout<< Variable/Constant /'Expression;

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

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

Variable/Constant/Expression  It is the variable, constant or expression whose value is display on the screen.

Input and output in c++

Examples

  cout<<” ZITOC.COM”;

The above line will display “ZITOC.COM” on the screen. The string constant is always enclosed in double-quotes.

  cout<<72;

The above line will display 72 on the screen. The numeric constant is not enclosed in quotation marks.

  cout<<a;

The above line will display the value of variable an on the screen The variables are not enclosed in quotation marks.

  cout<<”The value of Z”<<Z;

The above line will display ‘The value of Z along with the value of variable Z on the screen. The insertion operator is used twice to concatenate the string value with the value of Z.

Program:

Write a program that displays a message and value of integer and character variables.

  #include <iostream>
  #include <conio.h>
  using namespace std;
  void main() 
  {
	    int n = 15;
	    char z = ‘*’;
	    cout<<”Testing output……!”;
	    cout<<n;
	    cout<<z;
	    getch();
  }

Output:

Testing output…..!15*

How above Program Works

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

Program:

Write a program that adds Iwo floating-point numbers and shows Ow sum on screen.

  #include <iostream>
  #include <conio.h>
  using namespace std;
  void main() 
  {
	    float a,b,c,d;
	    a = 15.32;
	    b = 10.43;
	    c = a+b;
	    cout<<a <<” + ”<<b<<” = ”<<c;
	    getch();
  }

Output:

15.32 + 10.43 = 25.75

Program:

Write a program to calculate and punt the area of the square with a given height and width.

  #include <iostream>
  #include <conio.h>
  using namespace std;
  void main() 
  {
	    int height, width , area;
	    height = 8
	    width = 4;
	    area = height*width;
	    cout<<”Area Of Square =<<area;
	    getch();
  }

Output:

Area of Square = 32;

Working of above Program

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

Escape Sequences

Escape sequences are special characters used in the control string to modify the format of the output. These characters are not displayed in the output. These characters are used in combination with a backslash “/” The backslash is called escape character. Different escape sequences used in the C language are as follows:

Escape Sequence Purpose
\a Alarm
\b Backspace
\f From feed
\n Newline
\r Carriage return
\t Tab
\’ Single quote
\” Double quote

Escape sequence

\a

This escape sequence is used to play beep dung execution. For example:

  cout<<”Hello \a Zitoc”;

Will display: Hello Zitoc

After displaying “Hello” the computer bill play beep and then print “Zitoc

\b

The escape sequence is used to insert backspace in the output. For example:

  cout<<”Hello \b Zitoc”;

Will display: Hello Zitoc

First of all, ‘Hello’ is printed but \b deletes ‘o’ Then “Zitoc” is printed. So

“HelloZitoc” is displayed on the screen.

\f

This escape sequence is used to insert a blank paper in the printed output. It is called from feed. For example:

  cout<<”Hello \f Zitoc”;

Will display: Hello Zitoc

After printing ‘Hello’, the computer will include a blank paper and then print the remaining output. It is used during printing.

\n

This estate sequence is used to invert a new line in the output. for example:

  cout<<”Hello \n Zitoc”;

Will display

Hello

Zitoc

First of all, ‘Hello’ is printed and ‘\n’ shifts the cursor to the next line. Then ‘Zitoc

printed. The output is displayed on two line.

\r

This escape sequence is used to move the cursor at the beginning of the current line. For example:

  cout<<”Hello \r Zitoc”;

Will display: Zitoc

First of all. ‘Hello” is printed but \r moves the cursor at the beginning of current low After this, “Zitoc” is printed that otherwise ‘Hello’. So only “Zitoc” is displayed on the screen.

\t

This escape sequence is used to insert a TAB in the output. For example:

  cout<<”Hello \t Zitoc”;

will display

Hello            Zitoc

First of all, ‘Hello’ is printed and \t inserts a TAB. Then “Zitoc” Is printed. So it displays 

 ‘Hello                               World’ on the screen.

\’

This escape sequence is used to display single quotes in the output. For example:

  cout<<”\’Hello Zitoc\’”;

will display: ‘Hello Zitoc

\”

This escape sequence is used to display double quotes in the output. For example:

  cout<<”\”Hello Zitoc\””;

will display: “Hello Zitoc

\\

This escape sequence is used to display double backslash in the output. For example:

  cout<<”Z:\\ ”;

will display Z:\

Program:

Write a program to display the following output using single out statement.

*

* *

* * *

* * * *

  #include <iostream>
  #include <conio.h>
  using namespace std;
  void main() 
  {
	cout<<”*\n**\n***\n****”;
	getch();
  }

C++ Manipulators

C++ manipulators are used to formatting new output in different styles. The manipulators are the most common way to controi output formating.

Some important manipulators in C.+ are as follows:

The endl manipulator can be used by including iostream.h file in the program and setw, setprecision and setfill manipulators can be used by including iomanip.h file.

  • endl
  • setw
  • setprecision
  • setfill
  • showpoint
  • fixed

‘endl’ Manipulator

The word endl stands for end of the line. The endl manipulator is used to move cursor to the beginning of next line. It works similar to ‘\n’ escape sequence. It requires no parameter.

Example

  cout<<”Hello”<<endl<<”Zitoc”;

The above line will display the following output:

Hello

Zitoc.

The endl manipulator is not used as part of the string. It us wrote with a separate insertion operator (<<) without quotation marks. The above example first displays ‘Hello’. The endl manipulator displays a new line and then ‘Zitoc‘ is displayed on the next line.

‘setw’ Manipulator

The word ‘setw’ stands for set width. The setw manipulator is used to display the value of an expression in specified columns. The value of the expression can he string or number. If the value of the expression is less than specified columns, the additional columns are kit blank from the left side. The output automatically uses the required columns if the output is larger than the specified columns. The use of setw has no effect on the output in this case.

The setw manipulator is applied only to the value that is inserted after it. The output right-justified by default. The setw manipulator is part of lornanip.h.

Syntax

The syntax of ‘setw’ manipulator is as follows.’

setw(n)

The n indicates the number of columns in which the value is to be displayed. It can be an unsigned positive integer constant, variable or expression.

‘setprecision’ Manipulator

The ‘set precision’ manipulator is used to set the number of digits to be displayed after a decimal point. It is applied to all subsequent floating point numbers written to that output stream. The value is rounded with the use of this manipulator.

Syntax

 The syntax of ‘setprecision’ manipulator is as follows:

 setprecision(n)

The n indicates the number of digits displayed after the decimal point. It can be unsigned positive integer constant, variable or expression.

‘fixed’ Manipulator

The ‘fixed’ manipulator is used to further control the output of floating-point numbers.

It displays the floating-point numbers in a fixed decimal format. The following statement sets the output of floating-point numbers in a fixed decimal format:

  cout<<fixed:

All floating-point number after the above statement executes is displayed in the fixed decimal format until the manipulator fixed is disabled. It can be disabled by using the stream member function unsetf as follows:

  cout.unsetf(io:fixed);

‘showpoint’ Manipulator

The decimal part of a floating-point number is not displayed if the decimal part of the number is zero and the user displays the number using a fixed decimal format. The showpoint the manipulator is used to displays the decimal part even if the decimal part is zero.

The following statement setes the output numbers with a decimal point and trailing zeros on the standard input device.

program:

write a program that displays the values of different variables using showpoint manipulator.

  #include <iostream>
  #include <conio.h>
  #include <iomanip.h>
  void main()
  {
	double x,y,z;
	x = 15.676;
	y = 235.73;
	z = 9525.9864;
	cout.setf(ios::fixed, ios::floatfield);
	cout.setf(ios::showpoint);
	cout<<setprecision(2)<<”setpresion(2)”<<endl;
	cout<<”x = ”<<x<<endl;
	cout<<”y = ”<<y<<endl;
	cout<<”z = ”<<z<<endl;
	cout<<setprecision(3)<<”setpresion(3)”<<endl;
	cout<<”x = ”<<x<<endl;
	cout<<”y = ”<<y<<endl;
	cout<<”z = ”<<z<<endl;
	cout<<setprecision(4)<<”setpresion(4)”<<endl;
	cout<<”x = ”<<x<<endl;
	cout<<”y = ”<<y<<endl;
	cout<<”z = ”<<z<<endl;
	cout<<setprecision(3)<<x<<” ”;
	cout<<setprecision(2)<<y<<” ”<<setprecision(4)<<z<<endl;
	getch();
  }

Output:

setprecision(2)

x = 15.67

y = 235.73

z = 9525.99

setprecision(3)

x = 15.674

y = 235.730

z = 9525.986

setprecision(4)

x = 15.6740

y = 235.7300

z = 9525.9864

15.674          235.730       9525.986

‘setfill’ Manipulator

The setfill manipulators are used to replace the leading or trailing blank in the output by the specified character. It requires one parameters to specify the fill characters. The parameter can be a character constant, character variable or an integer that represents the fill character in the ASCII system. The manipulators must be used with fixed width outputs.

Example

The manipulators setfill (‘*’) or setfill (42) will replace the leadings or trailing blanks in the output by *. The value 42 is the decimal equivalent of the character ‘*’ in ASCII.

Example

  cout<<endl<<setfill ( ‘ * ’)<<”Result”;

The above statement will simply display the following:

R E S U L T S :

The above statement has not specified a field of fixed width to display the string. It will appear in the field of minimum width. It takes exactly eight fields to display the value. That is why the setfill (‘*’) has no effect.

Example

  cout<<endl<<sewt(15)<<setfill ( “ * ” )<<”Result”;

The above statement defines a field width of 15. It is more than the length of the value to be displayed. The statement will display the value as follows:

* * * * * * * R E S U L T S :

Standard Input

By default, the term standard input refers to the input given via the keyboard. C++ uses cin stream object to get standard input. The word ‘cin’ stands for console input. C++ handles standard input by applying the extraction operator (>>) with cin stream. The operator must be followed by a variable. The variable is used to store the input data.input and output defined in the following.

Syntax

The syntax of using cin object follows:

  cin>>var;

cin     It is the name of the object used to get standard input.

>>      It is known as an extraction operator or gets from the operator. It gets the input from cin object. The cin object then stores the input in the variable. The left side of >> operator must be an input stream like cin. The right side of >> operator must be a variable of the simple data type. One statement can use multiple extraction operators to get multiple values.

var    It is the variable in which the input value is stored.

Input and output in c++

Example:

  cin>>x;

The above line will get a value from the keyboard and store it in variable x.

  cin>>a>>b>>c;

The above line will get three values from the keyboard and store it in variable a, b and c.

Program:

Write a program that inputs name, age and address from the user and then displays these value on the screen.

  #include <iostream>
  #include <conio.h>
  #include <iomanip.h>
  void main()
  {
	char name[25],city[30];
	int age;
	cout<<”Enter Your Age : ”;
	cin>>age;
	cout<<”Enter your First Name : ”;
	cin>>name;
	cout<<”Enter your city : ”;
	cin>>city;
	cout<<”\nYour First Name is ”<<name<<endl;
	cout<<”Your city is ”<<city<<endl;
	cout<<”Your age is ”<<age<<endl;
	getch();
  }

Output:

Enter your age : 18

Enter Your First Name: Zitoc

Enter your city: Layyah

Your First Name is Zitoc.

Your city is Layyah

Your age is 

operator 

Advertisement