Advertisement
Functions | Description |
---|---|
width(int width) | Using this function, we can specify the width of a value to be displayed in the output at the console. |
fill(char ch) | Using this function, we can fill the unused white spaces in a value(to be printed at the console), with a character of our choice. |
setf(arg1, arg2) | Using this function, we can set the flags, which allow us to display a value in a particular format. |
peek() | The function returns the next character from the input stream, without removing it from the stream. |
ignore(int num) | The function skips over a number of characters, when taking an input from a user at the console. |
putback(char ch) | This function appends a character(which was last read by the get() function), back to the input stream. |
precision(int num_of_digts) | Using this function, we can specify the number of digits to the right of a decimal in a floating-point value to be printed in the output. |
width(int width);
Where, width is the width of a value to be displayed in the output at the console.
//C++ The width() formatted output function defines the width of a next value to be displayed in the output at the console.
#include<iostream>
using namespace std;
int main()
{
//First value
char ch = 'a';
//Adjusting the width of the next value to displayed to 5 columns.
cout.width(5);
//Adjusted width of the first value
cout<<ch <<"\n";
//Second value
int i = 1;
//Width of the second value to be displayed in the output will not be adjusted to 5 columns.
cout<<i;
}
a
1
Advertisement
fill(char ch);
Where, ch is a char value which fills the unused white spaces in a value.
//C++ The fill() formatted output function fills the unused white spaces in a value(to be printed at the console), with a character of our choice.
#include<iostream>
using namespace std;
int main()
{
//First value
char ch = 'a';
//Calling the fill function to fill the white spaces in a value with a character our of choice.
cout.fill('(');
//Setting the width of this value to 10
cout.width(10);
//Printing the first value
cout<<ch <<"\n";
//Second value
int i = 1;
//Once you call the fill() function, you don't have to call it again to fill the white space in a value with the same character.
//Setting the width of this value to 5
cout.width(5);
//Printing the second value
cout<<i;
}
(((((((((a
((((1
peek(void);
//C++ The peek() function is used to return the next character from the input stream, without removing it from the stream.
#include<iostream>
using namespace std;
int main()
{
//First char value
char d;
cout<<"Please enter a character to be read by the peek() function : ";
//Calling the peek() function to return the next character from input stream, without removing it from the stream
d = cin.peek();
//Printing the first char value
cout<<"The value in d is : " << d <<"\n";
//Second char value
char e;
//Calling the get() function to return the next character from input stream, and also removing it from the stream
e = cin.get();
//Printing the second char value
cout<<"The value in e is : " << e;
return 0;
}
Please enter a character to be read by peek() function : a
The value in d is : a
The value in e is : a
ignore(int num);
Where, num is the number of characters that will be skipped while taking an input from a user at the console.
//C++ ignore() functionto skip over a number of characters, when taking an input from the user at console.
#include<iostream>
using namespace std;
int main()
{
//A char array
char season[10];
//Prompting a user to enter the favorite season
cout<<"Enter the favorite season : ";
//Calling the ignore() function to skip over first 4 characters in the input from the user
cin.ignore(4);
//Taking the input from the user to fill the char array, season
cin>>season;
//Printing the value of char array, season
cout<<"Your favorite season : " << season <<"\n";
//A String
string city;
//Prompting a user to enter the favorite city name
cout<<"Enter your favorite city : ";
//Calling the ignore() function to skip over first 2 characters in the input from the user
cin.ignore(2);
//Taking the input from the user to fill the string, city
cin>>city;
//Printing the value of string, city
cout<<"The city you have entered : " << city;
return 0;
}
Enter the favorite season : Autumn
Your favorite season : mn
Enter the city : California
The city you have entered : alifornia
putback(char ch);
Where, ch is a char value appended back to the input stream.
//C++ putback() function is used to putback a character(which was last read by get() function) back to the input stream.
#include<iostream>
using namespace std;
int main()
{
//First char value
char c;
//Promting a user to enter a character value
cout<<"Please enter a character : ";
//Calling the get() function to take an input from the user and assign it to our first char value
c = cin.get();
//Printing the value of our first char value
cout<<"The value in c is : " << c << "\n";
//Calling the function putback(char c) to put back the last character read back to the input stream
cin.putback(c);
//Second char value
char d;
//Calling the get() function to take an input from the user and assign it to our second char value
d = cin.get();
//Printing the value of our second char value
cout<<"The value in d is : " << d;
return 0;
}
Please enter a character : a
The value in c is : a
The value in d is : a
setf(arg1, arg2);
Where, arg1 and arg2 are formatting flags which are defined in the ios class.
//C++ The setf() function is used to set the flags, which allow us to display a value in a particular format.
#include<iostream>
using namespace std;
int main()
{
//The first int
int a = 10;
//Calling the setf() function to set the flags to display a number, which is right-justified.
cout.setf(ios::right,ios::adjustfield);
//Calling the width() function to set the number of columns used to display a value at the console.
cout.width(10);
//Displaying the value of our first int value after calling the setf() and width() function
cout<< a << "\n";
//The second int
int b = 20;
//Calling setf() function to set the flags to display a number, which is left-justified.
cout.setf(ios::left,ios::adjustfield);
//Calling the width() function to set the number of columns used to display a value at the console.
cout.width(10);
//Displaying the value of our second int after calling the setf() and width() function
cout<< b;
return 0;
}
10
20
precision(int num_of_digits);
Where, num_of_digits is number of digits to the right of the decimal, to be printed in the output at the console.
//C++ The precision() function defines the number of digits to be diplayed after the decimal, after rounding off a floating number, at the console.
#include<iostream>
using namespace std;
int main()
{
//A floating-point value
float d = 12.3458998f;
//Printing the floating-point value
cout<< "The floating value in variable d : " << d <<"\n";
//Calling setf() function to set the flags to display a fixed number of digits after decimal in a floating-point value.
cout.setf(ios::fixed, ios::floatfield);
//Defining the number of digits to be displayed after the decimal in a floating number.
cout.precision(3);
//Printing the floating-point value after calling the setf() and precision() function
cout<< "The floating value in variable d, after calling the precision() function : " << d;
}
The floating value in variable d : 12.3459
The floating value in variable d, after calling the precision() function : 12.346
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement