Login


Lost your password?

Don't have an account ? Register ×
  
CPP All : Display Using IO Manipulation
Concept Learning Code Sheets

Use of setw() manipulator #4310

Demonstration of setw manipulation for setting display width

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char str[]="Hello";
cout<<setw(5)<<str<<endl;
cout<<setw(6)<<str<<endl;
cout<<setw(7)<<str<<endl;
cout<<str<<endl;
float f=3.1416;
cout<<f<<endl;
cout<<setw(10)<<f<<endl;
return 0;
}

Testing setw() for persistence of its effect #4312

Seeing in what condition effect of setw is persistent and when it is not.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char str[]="Hello";
cout<<setw(7)<<endl;
int a=10;
int b=15;
cout<<str<<endl;
cout<<str<<endl;
cout<<setw(10)<<str<<str<<endl;
cout<<setw(10)<<str<<setw(6)<<str<<endl;
return 0;
}

setf method for left and right alignment in a display field #4318

Use of setf method of cout to align content as left or right in a display field.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
char str[]="Hello";
cout.setf(ios::left);
cout<<setw(10)<<str<<setw(10)<<str<<endl;
cout.unsetf(ios::left);
cout<<setw(10)<<str<<setw(10)<<str<<endl;
cout.setf(ios::right);
cout<<setw(10)<<str<<setw(10)<<str<<endl;
return 0;
}

using setprecision without fixed or scientific format. #4326

setprecision to set number of digits for any decimal containing floating number.

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double d=123.5613;
cout<<d<<endl;
cout<<setprecision(4)<<d<<endl;
cout<<setprecision(7)<<d<<endl;
cout<<setprecision(8)<<d<<endl;
cout<<setprecision(3)<<d<<endl;
cout<<setprecision(2)<<d<<endl;
return 0;
}

Solved Problems

problem based on setw, setf and unsetf #4321

print the following table of temperature record of a patient properly using


Code Sheets:4  Solved Problems:1 
Back