Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 L7.

Similar presentations


Presentation on theme: "Chapter 3 L7."— Presentation transcript:

1 Chapter 3 L7

2 Formatting output L7 Manipulator (flags) setprecision showpoints
setleft setright setw stream

3 3.8 Formatting Output Can control how output displays for numeric, string data: size position number of digits Requires iomanip header file

4 Precision and formatted output
How many values appear after and before the decimal point ?? cout<< setiosflags(ios::fixed | ios :: showpoint)<< setprecision (2); Or cout<< fixed << showpoint<< setprecision (2); Defined in the <iomanip.h> library

5 #include <iostream>
#include <iomanip> Using namespase std; int main() { float x; cout << "x = "; cin>> x; cout << setiosflags (ios :: fixed | ios :: showpoint | ios:: left)<<setprecision(2); // or // cout << fixed << showpoint<< left <<setprecision(2); cout << x ; Return 0; } Input Outout

6 Stream Manipulators Used to control features of an output field
Some affect just the next value displayed: setw(x): print in a field at least x spaces wide. Use more spaces if field is not wide enough Some affect values until changed again: fixed: use decimal notation for floating-point values setprecision(x): when used with fixed, prints floating-point value using x digits after the decimal. Without fixed, prints floating-point value using x significant digits // show an example of both Mix18.cpp showpoint: always print decimal for floating-point values )

7 Manipulator Examples const double e = 2.718;
cout << setw(8) << e; // in a // field 8 wide cout << setprecision(2); cout << e; // 2.7 cout << fixed<<setprecision(2); cout << e; // 2.72 double price = 25.0, discount = 0.6; cout << fixed << setprecision(2); cout << price * discount; // 15.00

8 Formatting output stream manipulator setw()
int Numb1 = 100, Numb2 = 200, Numb3 = 300; cout << Numb1 <<Numb2 <<Numb3; Gives us BUT: cout << setw(5 )<<Numb1<<setw(7) << Numb2<<setw(5)<<Numb3 SS100SSSS200SS300

9 The left and right Manipulators “right is the default”
float Miles = ; cout << setw(12)<<Miles; SSSSS cout << setiosflags (ios::left) <<setw(12) << Miles; cout<<left<<setw(12)<<Miles; SSSSS cout << setiosflags(ios::right) << setw(12) << Miles;

10 Formatting output setprecision manipulator setprecision()
float Num1 = ; cout << Num1; cout << fixed<<setprecision(4)<<Num1; cout << fixed<<setprecision(5)<<Num1; cout <<fixed<<setprecision(6)<<Num1;

11 BIG difference between set precision in Turbo and .net compilers
Look at table 3-11 page 109 Compare with the outcome of this program. #include<iostream.h> #include<iomanip.h> int main() { float Num1 = ; cout<< setiosflags(ios::fixed)<< setprecision (3) << Num1; return 0; } Output on turbo and DEV, 28.9 on dot net !!

12 Formatting output setprecision manipulator setprecision()
float Num1 = ; cout << setprecision(2)<<Num1; 2.1e+03 cout << setprecision(12)<<Num1; !!!!

13 Formatting output manipulator setprecision + fixed()
Forces cout to print the digits in fixed point notation or decimal. float Num1 = ; cout<< setiosflags(ios::fixed)<< setprecision (2) << Num1; 210.12 cout<< setiosflags(ios::fixed)<< setprecision (4) << Num1;

14 Formatting output manipulator showpoint()
Forces cout to display the trailing zeros. float Num1 = ; cout << Num1; 210 cout<< setiosflags(ios :: showpoint)<< Num1; NOTE: Cout <<setprecision(2)<<showpoint; // works for .net AND FOR DEV but not for Turbo

15 showpoint and setprecision
float Num1 = ; cout<< setprecision(6)<< Num1; 210 // would not show the 0000 cout setsettiosflags (ios::showpoint) <<setprecision(6)<< Num1;

16 showpoint and setprecision
causes the decimal points and trailing zeros to be displayed EVEN if there is no fractional part float Num1 = 210 ; cout<< setiosflags(ios :: showpoint) << setprecision(6) << Num1;

17 The fill( ) function cout.fill (‘x’)
Changes the fill character, whicjh is by default a space to be something else, ie x. float Miles = ; cout << setiosflags(ios :: right) << setw(12) << Miles; SSSSS cout.fill('x'); cout << setiosflags(ios :: right) << setw(12) <<Miles; xxxxx

18 setw() and strings cout<<“I Love C++ Programming”;
Will give you: I Love C++ Programming cout<<setw(22)<<“I Love C++ Programming”; Would give you the same, as long as its 22, or less. cout<<setw(30)<<“I Love C++ Programming”; SSSSSSSSI Love C++ Programming // right justified

19 When the size of the setw() less or = the size of the string
// This program demonstrates the setw manipulator being // used with values of various data types. #include <iostream> #include <iomanip> using namespace std; int main() { int intValue = 3928; double doubleValue = 91.5; const int ARRAY_SIZE = 14; char cStringValue[ARRAY_SIZE] = "John J. Smith"; cout << "(" << setw(5) << intValue << ")" << endl; cout << "(" << setw(8) << doubleValue << ")" << endl; cout << "(" << setw(16) << cStringValue << ")" << endl; // cout << "(" << setw(16) << cStringValue << ")" << endl system("pause"); return 0; }

20 Program example// notice the changes from yours
// we will run this program to demonstrate how flags work // if statments and loops wil be covered soon #include <iostream> #include <iomanip> using namespace std; int main() { float weight; int i; for (i = 0; i < 6; i++) cout<< fixed << showpoint<< setprecision (2); //cout<< setiosflags(ios :: fixed |ios :: showpoint)<< setprecision (2); cout << " \n enter weight \n" ; cin >> weight; if (weight > 150 ) cout << weight << setw(3)<< " over weight "; else if (weight > 100 && weight < 150) cout<< weight<<setw(3) << " no risk "; else cout << weight<<setw(3)<<" too thin "; } return 0;


Download ppt "Chapter 3 L7."

Similar presentations


Ads by Google