8. Managing console I/O operations

Console input / output function take input from standard input devices and compute and give output to standard output device.

Generally, keyboard is standard input device and monitor is standard output device.

In case of C++ it uses streams to perform input and output operations in standard input output devices (keyboard and monitor). A stream is an object which can either insert or extract the character from it.

The standard C++ library is iostream and standard input / output functions in C++ are

    1. cin

    2. cout

There are mainly two types of consol I/O operations form:

  1. Unformatted consol input output
  2. Formatted consol input output

1) Unformatted consol input output operations

These input / output operations are in unformatted mode. The following are operations of unformatted consol input / output operations:

A) void get()

It is a method of cin object used to input a single character from keyboard. But its main property is that it allows wide spaces and newline character.

Syntax:

        char c=cin.get();

Example:
#include<iostream>
using namespace std;

int main()
{
char c=cin.get();
cout<<c<<endl;

return 0;
}


Output:-
             r

             r

B) void put()

It is a method of cout object and it is used to print the specified character on the screen or monitor.

Syntax:-
       cout.put(variable / character);

Example:-
#include<iostream>
using namespace std;

int main()
{
char c=cin.get();
cout.put(c);     //Here it prints the value of variable c;
cout.put('c');   //Here it prints the character 'c';

return 0;
}


Output:-
c
c

C) getline(char *buffer,int size)

This is a method of cin object and it is used to input a string with multiple spaces.

Syntax:-
         char x[30];
         cin.getline(x,30);

Example:-
#include<iostream>
using namespace std;

int main()
{
cout<<"Enter name :";
char c[10];
cin.getline(c,10);   //It takes 10 charcters as input;
cout<<c<<endl;

return 0;
}
Output

Enter name :Rupesh
Rupesh


D) write(char * buffer, int n)

It is a method of cout object. This method is used to read n character from buffer variable.

Syntax:-
      cout.write(x,2);

Example:-
#include<iostream>
using namespace std;

int main()
{
cout<<"Enter name : ";
char c[10];
cin.getline(c,10);     //It takes 10 charcters as input;
cout.write(c,9);      //It reads only 9 character from buffer c; 

return 0;
}


Output:-
Enter name : Rupesh
Rupesh


E) cin

It is the method to take input any variable / character / string.

Syntax:-
        cin>>variable / character / String / ;

Example:-
#include<iostream>
using namespace std;

int main()
{
    int num;
    char ch;
    string str;
    
    cout<<"Enter Number"<<endl;
    cin>>num; 
    cout<<"Enter Character"<<endl;
    cin>>ch; 
    cout<<"Enter String"<<endl;
    cin>>str; 
    
    return 0;
}


Output:-
Enter Number
07
Enter Character
r
Enter String
Rupesh


F) cout
This method is used to print variable / string / character.

Syntax:-
    cout<< variable / charcter / string;

Example:-
#include<iostream>
using namespace std;

int main()
{
    int num=100;
    char ch='X';
    string str="Deepak";
    
    cout<<"Number is "<<num<<endl; 
    cout<<"Character is "<<ch<<endl; 
    cout<<"String is "<<str<<endl; 
    
    return 0;
}


Output:-
        Number is 100
        Character is X
        String is Rupesh

2) Formatted console input output operations

In formatted console input output operations we uses following functions to make output in perfect alignment. In industrial programming all the output should be perfectly formatted due to this reason C++ provides many function to convert any file into perfect aligned format. These functions are available in header file <iomanip>. iomanip refers input output manipulations.

A). width(n)
This function is used to set width of the output.

Syntax:-

       cout<<setw(int n);

Example:-
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int x=10;
cout<<setw(20)<<variable;

return 0;
}


Output:-
             10


B). fill(char)

This function is used to fill specified character at unused space.

Syntax:-

       cout<<setfill('character')<<variable;

Example:-
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
int x=10;
cout<<setw(20);
cout<<setfill('#')<<x;

return 0;
}
Output

##################10 

C). precison(n)
    This method is used for setting floating point of the output.

Syntax:-
    cout<<setprecision('int n')<<variable;


Example:-
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
float x=10.12345;
cout<<setprecision(5)<<x;

return 0;
}


Output:-
       10.123


D). setflag(arg 1, arg,2)

This function is used for setting format flags for output.


Syntax:-
        setiosflags(argument 1, argument 2);

E). unsetflag(arg 2)

This function is used to reset set flags for output.

Syntax:-
          resetiosflags(argument 2);

F). setbase(arg)

This function is used to set basefield of the flag.

Syntax:-
      setbase(argument);

I/O Stream 

What is Stream?

  • A stream is an abstraction. It is a sequence of bytes.
  • It represents a device on which input and output operations are performed.
  • It can be represented as a source or destination of characters of indefinite length.
  • It is generally associated to a physical source or destination of characters like a disk file, keyboard or console.
  • C++ provides standard iostream library to operate with streams.
  • The iostream is an object-oriented library which provides Input/Output functionality using streams.




Advantages of Stream Classes:-
  • Stream classes have good error handling capabilities.
  • These classes work as an abstraction for the user that means the internal operation is encapsulated from the user.
  • These classes are buffered and do not uses the memory disk space.
  • These classes have various functions that make reading or writing a sequence of bytes easy for the programmer.

Post a Comment

0 Comments