Download presentation
Presentation is loading. Please wait.
Published byNathan Clarke Modified over 6 years ago
1
Formatting Output iomanip.h Objects Escape Sequences setw()
setiosflags(…) setprecision()
2
Output ¾ cout The object cout is used to direct data to the ststandard output device, usually the monitor. w predefined stream object w in iostream library w used with insertion operator << *
3
Output ¾ cout Syntax: cout << ExprOrString;
Read << as “put to”. cout << 24; cout << ‘y’; cout << “Hello”; *
4
Output ¾ cout Syntax: cout << ExprOrString;
cout << “The answer to question “; cout << qu_num; cout << “ is “; cout << 1.5 * pay_rate; *
5
Output ¾ cout Syntax: cout << ExprOrString << ExprOrString; Each appends data to the stream. cout << “Num1 * num2 is “ << num1*num2; OUTPUT Num1 * num2 is 110 space * *
6
Output ¾ cout Syntax: cout << ExprOrString << ExprOrString; Each appends data to the stream. cout << “Num =“ << 35.1; OUTPUT Num =35.1 no space * *
7
Output ¾ cout Syntax: cout << ExprOrString << ExprOrString; Each appends data to the stream. cout << “Hi, “ << name << “how are you?”; no space space OUTPUT Hi, Shadowhow are you? * * *
8
Output ¾ cout Syntax: cout << ExprOrString << ExprOrString;
cout << “The answer to question “; cout << qu_num; cout << “ is “; cout << 1.5 * pay_rate; no ; cout << “The answer to question “ << qu_num << “ is “<< 1.5 * pay_rate; * *
9
Output ¾ cout Syntax: cout << ExprOrString << ExprOrString
Output ¾ cout 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 ; * *
10
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.
11
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”; OUTPUT Name Total Grade Miyo B Jake A Syd A _ \n \n gives a blank line *
12
Escape Sequences \ Changes the meaning of the character that follows it. \” take quotes literally “Alfonsus \”Butch\” Billone” gives Alfonsus ”Butch” Billone *
13
Escape Sequences “\n Alfonsus\n\”Butch\” \nBillone” gives
Alfonsus “Butch” Billone “|\n Alfonsus\n \t \”Butch\” \tBillone|” gives | Alfonsus “Butch” Billone| * * * *
14
Formatting Output endl
does the same thing as \n - inserts a hard return requires the insertion operator cout << endl << endl; is the same as: cout << “\n\n”;
15
Formatting Output iomanip.h contains the objects which have special effects on the iostream. #include <iostream.h> #include <iomanip.h> *
16
Formatting Output iomanip
Formatting Output iomanip.h contains the objects which have special effects on the iostream.…… …... setw(n) how many columns for data setprecision(n) sets number of decimals setiosflags(ios::fixed) displays 6 digits after the decimal * * *
17
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 ¨o33¨7132¨oHi ¨Hi * *
18
Formatting Output setprecision(n) sets number of decimals
x = format result setw(10) setprecision(2) ¨ooo314.00 point counts setw(10) setprecision(5) ¨ columns added setw(7) setprecision(5) * * *
19
Formatting Output setiosflags(ios::fixed) displays 6 digits after
Formatting Output setiosflags(ios::fixed) displays 6 digits after the decimal #include<iostream.h> #include<iomanip.h> void main() { double v = ; double w = ; double x = ; double y = ; double z = ;
20
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 cout << setprecision(2); }
21
Formatting Output default 0.00123457 1.23457 12.3457 1234.57 12345.7
ios::fixed + setprecision(2) 0.00 1.23 12.35
22
Formatting Output For decimal alignment use:
ios::fixed and setprecision( ) sitcky setw( ) only once cout << setiosflags(ios::fixed) << setprecision(n); l l l cout << “- -” << setw(n) << var1 << setw(n) << “- -”; cout << “- -\t” << setw(n) << var1 << setw(n) <<var2; *
23
cout Syntax cout << ExprOrStringOrManipulator << ExprOrStringOrManipulator...;
24
Good luck is nothing but prepardness and opportunity coming together.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.