C Streams
C Streams
C Streams
7, 8
Southern Technical University Programming in C ++ course code CST100
Technical Institute / Qurna 0 lecturer: Israa Mahmood Hayder
Console input / output function take input from standard input devices and
compute and give output to standard output device.
The standard C++ library is iostream and standard input / output functions in
C++ are:
1. cin
2. cout
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
I
I
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);
Lecture No. 7, 8
Southern Technical University Programming in C ++ course code CST100
Technical Institute / Qurna 0 lecturer: Israa Mahmood Hayder
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
I
Ic
Syntax:
char x[30];
cin.getline(x,30);
Example:
#include<iostream>
using namespace std;
int main()
{
cout<<"Enter name :";
Lecture No. 7, 8
Southern Technical University Programming in C ++ course code CST100
Technical Institute / Qurna 0 lecturer: Israa Mahmood Hayder
char c[10];
cin.getline(c,10); //It takes 10 charcters as input;
cout<<c<<endl;
return 0;
}
Output
Enter name :Divyanshu
Divyanshu
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
Lecture No. 7, 8
Southern Technical University Programming in C ++ course code CST100
Technical Institute / Qurna 0 lecturer: Israa Mahmood Hayder
E) cin
It is the method to take input any variable / character / string.
Syntax:
Example:
#include<iostream>
using namespace std;
int main()
{
int num;
char ch;
string str;
cout<<"Enter Number"<<endl;
cin>>num; //Inputs a variable;
cout<<"Enter Character"<<endl;
cin>>ch; //Inputs a character;
cout<<"Enter String"<<endl;
cin>>str; //Inputs a string;
return 0;
}
Output
Enter Number
07
Enter Character
h
Enter String
Lecture No. 7, 8
Southern Technical University Programming in C ++ course code CST100
Technical Institute / Qurna 0 lecturer: Israa Mahmood Hayder
Deepak
F) cout
This method is used to print variable / string / character.
Syntax:
Example:
#include<iostream>
using namespace std;
int main()
{
int num=100;
char ch='X';
string str="Deepak";
return 0;
}
Output
Number is 100
Character is X
String is Deepak
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;
Lecture No. 7, 8
Southern Technical University Programming in C ++ course code CST100
Technical Institute / Qurna 0 lecturer: Israa Mahmood Hayder
Example:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int x=10;
cout<<setw(20);
cout<<setfill('#')<<x;
return 0;
}
Output
##################10
D) 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;
}
Lecture No. 7, 8
Southern Technical University Programming in C ++ course code CST100
Technical Institute / Qurna 0 lecturer: Israa Mahmood Hayder
Output
10.123
E) setflag(arg 1, arg,2)
This function is used for setting format flags for output.
Syntax:
F) unsetflag(arg 2)
This function is used to reset set flags for output.
Syntax:
resetiosflags(argument 2);
G) setbase(arg)
This function is used to set basefield of the flag.
Syntax:
setbase(argument);