formatting output
DESCRIPTION
Formatting Output. Escape Sequences iomanip.h Objects setw() setiosflags(…) setprecision(). Output ¾ cout The object cout is used to direct data to the st standard output device, usually the monitor. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/1.jpg)
Formatting Output
Escape Sequences iomanip.h Objects
setw() setiosflags(…) setprecision()
![Page 2: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/2.jpg)
Output cout
The object cout is used to direct data to the ststandard output device, usually the monitor.
predefined stream object in iostream library used with insertion operator <<
*
![Page 3: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/3.jpg)
Output cout
Syntax: cout << ExprOrString;
Read << as “put to”.
cout << 24;
cout << ‘y’;
cout << “Hello”;
Note: none of these contain a “\n” or endl, and
thus may not be displayed immediately.
*
![Page 4: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/4.jpg)
Output cout
Syntax: cout << ExprOrString;
cout << “The answer to question “;
cout << q_num;
cout << “ is “;
cout << 1.5 * pay_rate;
cout << endl; // force a new line and flush
*
The answer to question 5 is 9.0
q_num
5pay_rate
6
![Page 5: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/5.jpg)
Output cout
Syntax: cout << ExprOrString << ExprOrString;
Each appends data to the stream.
cout << “Num1 * num2 is “ << num1*num2;
OUTPUT
Num1 * num2 is 110
* *
space
![Page 6: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/6.jpg)
Output cout
Syntax: cout << ExprOrString << ExprOrString;
Each appends data to the stream.
cout << “Num =“ << 35.1;
OUTPUT
Num =35.1
* *
no space
![Page 7: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/7.jpg)
Spaces – Essential for Strings
Syntax: cout << ExprOrString << ExprOrString;
Each appends data to the stream.
cout << “Hi, “ << name << “how are you?”;
no spacespace
OUTPUT
Hi, Shadowhow are you?
* * *
![Page 8: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/8.jpg)
Output cout
Syntax: cout << ExprOrString << ExprOrString;
cout << “The answer to question “;cout << q_num;cout << “ is “;cout << 1.5 * pay_rate;
* *
no ;
cout << “The answer to question “ << qu_num << “ is “<< 1.5 * pay_rate;
align the put-to operators for good style
![Page 9: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/9.jpg)
More Multi-line Syntax Syntax: cout << ExprOrString << ExprOrString
<< ExprOrString ;
cout << “The answer to question number 17 is listed after question “;cout << lastqu << ‘.’;
* *
cout << “The answer to question number 17 “<< “is listed after question “ << lastqu<< ‘.’;
no ;
Strings should never span multiple lines error
![Page 10: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/10.jpg)
Escape Sequences \ Changes the meaning of the
character that follows it.
\n hard return\t tab\\ \\” “\a beep or bell
These must be within quotation marks.
![Page 11: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/11.jpg)
Escape Sequences
cout << “Name\tTotal\tGrade\n”;cout << “Miyo\t186\t B\n”;cout << “\n Jake\t211\t A\n”;cout << “Syd\t203\t A\n”;
OUTPUTName Total GradeMiyo 186 B
Jake 211 ASyd 203 A_ *
\n \n gives a blank line
![Page 12: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/12.jpg)
Escape Sequences
\ Changes the meaning of the character that follows it.
\” take quotes literally
*
“James \”Big Jim\” Traficant”gives
James “Big Jim” Traficant
![Page 13: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/13.jpg)
Escape Sequences
* * * *
“\n Alfonsus\n\”Butch\” \nBillone”gives
Alfonsus“Butch”Billone
“|\n Alfonsus\n \t \”Butch\” \tBillone|”gives
|Alfonsus
“Butch” Billone|
![Page 14: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/14.jpg)
Formatting Output
endlendl does the same thing as does the same thing as \n\n
- inserts a hard return- inserts a hard return requires the insertion operatorrequires the insertion operator
cout << endl << endlcout << endl << endl;;is the same as:is the same as:
cout << “\n\n”;cout << “\n\n”;
![Page 15: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/15.jpg)
2.3 Numerical Output using cout
Using cout to output the results of an arithmetic expression
• Numerical output should be displayed attractively
• Programs are often judged on the presentation of their output, (in addition to the accuracy of the results)
• For example, $1.897 is not acceptable output
![Page 16: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/16.jpg)
cout Syntax
cout << ExprOrStringOrManipulator << ExprOrStringOrManipulator...;
For Example:cout << setw(5) << 350; uses field width of 5cout << setw(7) << 350; uses field width of 7
![Page 17: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/17.jpg)
Formatting Output
the iomanip header file contains objects which have special effects on the iostream.
*
#include <iostream>#include <iomanip>
![Page 18: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/18.jpg)
setw(n) how many columns for data
setprecision(n) sets number of decimals
setiosflags(ios::fixed) displays 6 digits after the decimal
* * *
Formatting Output
iomanip contains the objects which havespecial effects on the iostream.…… …...
![Page 19: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/19.jpg)
Formatting Output
setw(n) how many columns for data
cout << setw(4) << ans cout << setw(1) << ans<< setw(5) << num << setw(3) << num<< setw(4) << “Hi”; << setw(3) << “Hi”;
* *fields expand
337132Hi 337132Hi
![Page 20: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/20.jpg)
Formatting Output
setprecision(n) sets number of decimals
x = format result314.0 setw(10)
setprecision(2)314.00
point counts
314.0 setw(10)setprecision(5) 314.00000
* * *columns added
314.0 setw(7)setprecision(5) 314.00000
![Page 21: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/21.jpg)
Formatting Output
setiosflags(ios::fixed) displays 6 digits after the decimal
#include<iostream>#include<iomanip>using namespace std;
void main(){
double v = 0.00123456789; double w = 1.23456789; double x = 12.3456789; double y = 1234.56789; double z = 12345.6789;
![Page 22: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/22.jpg)
Formatting Output
cout <<v<< endl<<w<< endl <<x<< endl <<y<< endl <<z<<”\n\n”;
cout << setiosflags(ios::fixed);cout <<v<< endl <<w<< endl <<x<< endl
<<y<< endl <<z<<”\n\n”;
cout << setprecision(2);cout <<v<< endl <<w<< endl <<x<< endl
<<y<< endl <<z<<”\n\n”;}
![Page 23: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/23.jpg)
Formatting Output
default
0.00123457
1.23457
12.3457
1234.57
12345.7
ios::fixed
0.001235
1.234568
12.345679
1234.567890
12345.678900
+ setprecision(2)
0.00
1.23
12.35
1234.57
12345.68
![Page 24: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/24.jpg)
Numerical output with cout
An example from the 2nd Edition text:
#include <iostream>using namespace std;int main(){cout << 6 << ‘\n’
<< 18 << ‘\n’<< 124 << ‘\n’<< “---\n”<< 6+19+124 << ‘\n’;
return 0;}
output
618124---149
![Page 25: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/25.jpg)
Numerical output with coutSame example using setw( ):
#include <iostream>#include <iomanip>using namespace std;int main(){cout << setw(3) << 6 << ‘\n’
<< setw(3) << 18 << ‘\n’<< setw(3) << 124 << ‘\n’<< “---\n”<< 6+19+124 << ‘\n’;
return 0;}
output
6 18124---149
![Page 26: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/26.jpg)
Formatting Output
For decimal alignment use:For decimal alignment use:
ios::fixed and setprecision( )setw( ) use setw( ) each use setw( ) each
timetime
cout << setiosflags(ios::fixed) << setprecision(n);cout << setiosflags(ios::fixed) << setprecision(n);
cout << “- -” << setw(n) << var1 << setw(n) << “- -”;cout << “- -” << setw(n) << var1 << setw(n) << “- -”;
cout << “- -\t” << setw(n) << var1 << setw(n) <<var2;cout << “- -\t” << setw(n) << var1 << setw(n) <<var2;*
![Page 27: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/27.jpg)
Common Programming Errors, cout and Variables
Forgetting to declare all variables
Storing wrong data type into a variable
Forgetting to initialize variables
Dividing integer values incorrectly
Mixing data types without knowing effect
Forgetting insertion symbol <<
![Page 28: Formatting Output](https://reader034.vdocument.in/reader034/viewer/2022051316/56814a58550346895db77e4f/html5/thumbnails/28.jpg)
Good luck is nothing
but preparednesspreparedness and
opportunityopportunity coming
together.