1 / 28

Formatting Output

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.

hilda
Download Presentation

Formatting Output

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Formatting Output • Escape Sequences • iomanip.h Objects • setw() • setiosflags(…) • setprecision()

  2. Output ¾ coutThe object cout is used to direct data to the ststandard output device, usually the monitor. • w predefined stream objectw in iostream libraryw used with insertion operator << *

  3. Output ¾ coutSyntax: 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. *

  4. Output ¾ coutSyntax: 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 q_num 5 pay_rate 6 The answer to question 5 is 9.0 *

  5. space Output ¾ coutSyntax: cout << ExprOrString << ExprOrString;Each appends data to the stream. • cout << “Num1 * num2 is “ << num1*num2; • OUTPUTNum1 * num2 is 110 * *

  6. no space Output ¾ coutSyntax: cout << ExprOrString << ExprOrString;Each appends data to the stream. • cout << “Num =“ << 35.1; • OUTPUTNum =35.1 * *

  7. space no space Spaces – Essential for StringsSyntax: cout << ExprOrString << ExprOrString;Each appends data to the stream. • OUTPUT Hi, Shadowhow are you? cout << “Hi, “ << name << “how are you?”; * * *

  8. no ; cout << “The answer to question “ << qu_num << “ is “<< 1.5 * pay_rate; Output ¾ coutSyntax: cout << ExprOrString << ExprOrString; • cout << “The answer to question “;cout << q_num;cout << “ is “;cout << 1.5 * pay_rate; align the put-to operators for good style * *

  9. no ; cout << “The answer to question number 17 “ << “is listed after question “ << lastqu << ‘.’; More Multi-line SyntaxSyntax: cout << ExprOrString << ExprOrString << ExprOrString ; • cout << “The answer to question number 17 is listed after question “;cout << lastqu << ‘.’; Strings should never span multiple lines error * *

  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. \n \n gives a blank line 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 186 B Jake 211 A Syd 203 A_ *

  12. Escape Sequences • \ Changes the meaning of the character that follows it.\” take quotes literally “James \”Big Jim\” Traficant” gives James “Big Jim” Traficant *

  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 • does the same thing as \n - inserts a hard return • requires the insertion operator cout << endl << endl; is the same as:cout << “\n\n”; endl

  15. 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

  16. cout Syntax • cout << ExprOrStringOrManipulator << ExprOrStringOrManipulator...; • For Example: • cout << setw(5) << 350; uses field width of 5 • cout << setw(7) << 350; uses field width of 7

  17. Formatting Output • the iomanip header file contains objects which have special effects on the iostream. #include <iostream>#include <iomanip> *

  18. Formatting Outputiomanip contains the objects which havespecial 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 * * *

  19. ¨o33¨7132¨oHi 337132¨Hi fields expand Formatting Outputsetw(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”; * *

  20. x = format result314.0 setw(10) setprecision(2) ¨ooo314.00 point counts 314.0 setw(7) setprecision(5) 314.00000 columns added Formatting Outputsetprecision(n) sets number of decimals • 314.0 setw(10) setprecision(5) ¨314.00000 * * *

  21. Formatting Outputsetiosflags(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;

  22. 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”; • }

  23. 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

  24. output 6 18 124 --- 149 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; • }

  25. output 6 18 124 --- 149 Numerical output with cout • Same 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; • }

  26. Formatting Output • For decimal alignment use: • ios::fixed and setprecision( ) • setw( ) use setw( ) each time cout << setiosflags(ios::fixed) << setprecision(n); l l l cout << “- -” << setw(n) << var1 << setw(n) << “- -”; cout << “- -\t” << setw(n) << var1 << setw(n) <<var2; *

  27. 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 <<

  28. Good luck is nothing but preparedness and opportunity coming together.

More Related