problem based on setw, setf and unsetf – Computer Sir Ki Class
  

Login
[lwa]



Solved Problem #CPP#4321    siteicon   siteicon  

Problem Statement - problem based on setw, setf and unsetf

print the following table of temperature record of a patient properly using setw,setf,unsetf methods.

 

Readings   Values
-----------------
Morning     100.4
Afternoon    99.8
Evening      98.8

 

Solution

TC++ #4321

#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
int main()
{
clrscr();
cout<<"Readings Values"<<endl;
cout<<"-----------------"<<endl;
cout.setf(ios::left);
cout<<setw(12)<<"Morning";
cout.setf(ios::right);
cout<<setw(5)<<"100.4"<<endl;
cout.unsetf(ios::right);
cout.setf(ios::left);
cout<<setw(12)<<"Afternoon";
cout.setf(ios::right);
cout<<setw(5)<<"99.8"<<endl;
cout.unsetf(ios::right);
cout.setf(ios::left);
cout<<setw(12)<<"Evening";
cout.setf(ios::right);
cout<<setw(5)<<"98.8"<<endl;
getch();
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout<<"Readings Values"<<endl;
cout<<"-----------------"<<endl;
cout.setf(ios::left);
cout<<setw(12)<<"Morning";
cout.setf(ios::right);
cout<<setw(5)<<"100.4"<<endl;
cout.unsetf(ios::right);
cout.setf(ios::left);
cout<<setw(12)<<"Afternoon";
cout.setf(ios::right);
cout<<setw(5)<<"99.8"<<endl;
cout.unsetf(ios::right);
cout.setf(ios::left);
cout<<setw(12)<<"Evening";
cout.setf(ios::right);
cout<<setw(5)<<"98.8"<<endl;
return 0;
}

Test it !

Run Output

Readings   Values
-----------------
Morning 100.4
Afternoon 99.8
Evening 98.8

Common Errors

  • Commonly learners forget that setf has a persistent behaviour unlike setw.


Suggested Filename(s): setwsetf.cpp



Share

sunmitra| Created: 15-May-2018 | Updated: 15-Sep-2018|






Back