1 / 25

Printing Numbers

Floating point numbers are printed out in some default format, which is usually not how you want them to print out. For example, if you want to print out dollars and cents: double price = 78.50; cout << “Price is $” << price << endl; You might see Price is $78.5. Printing Numbers.

takoda
Télécharger la présentation

Printing Numbers

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. Floating point numbers are printed out in some default format, which is usually not how you want them to print out. For example, if you want to print out dollars and cents: double price = 78.50; cout << “Price is $” << price << endl; You might see Price is $78.5 Printing Numbers

  2. If that's not what you want, you can use formatting commands. The following commands tell the computer to use a fixed format (rather than scientific notation), to always show the decimal point, and to show two digits to the right of the decimal point: cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); Formatting Numbers

  3. You can also specify that something be printed out in a field of a given size, that is, it will always take up a certain number of columns in the output, padding with spaces if necessary. You must include the iomanip library to do so and use the setw(int) modifer. #include <iomanip> int main(void) { cout << setw(10) << 12 << endl; } More Formatting

  4. #include <iostream> #include <iomanip> using namespace std; int main(void) { double price = 78.5; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << “Price is $” << setw(10) << price << endl; } gives Price is $ 78.50 Formatting Example

  5. Comments are human-readable lines of text which can be used to document your program. The computer ignores comments. There are two ways to indicate a comment. Two slashes (“//”) indicate that everything that follows them until the end of the line is a comment. A block of text is treated as a comment if it begins with “/*” and ends with “*/”. Comments

  6. /* This program was written by Don Simon on 01/15/2010 for the COSC 160 class */ #include <iostream> // library using namespace std; // namespace Example Comments

  7. For this class, each program should have the following documentation: Author's name, date, name of program Short program description Description of each variable (if not obvious) Each function and method should have a short description, including the inputs and outpus Description of major sections of code Details of any tricky (non-obvious) code Documentation Standards

  8. The idea is that some one who reads the code should understand what is going on. The comments should not mimic the code, that is, restate the code in English, but should provide the gist of what each major section of code does. For example: a = a + 1; // add 1 to a. is not an appropriate comment, but /* Read in the students' test scores and find the average */ is. Documentation Standards (cont'd)

  9. Namespaces are used to segment identifiers so that different programmers and different modules or libraries can use the same name without confusion. You can create your own namespaces, but for now, it is enough to know how to use a namespace. All standard libraries use the std namespace, so your programs should include the line: using namespace std; Namespaces

  10. The C++ system is divided into a core language and a set of libraries. The libraries provide additional functionality. One of the commonly used libraries contains the I/O routines. Another has functions for manipulating how things are printed out. To use a library, you must have an #include line near the top of your program to load the library functions: #include <iostream> #include <iomanip> Libraries

  11. As noted before, you can use arithmetic operators in expressions to manipulate numeric values: a + b * 7 – (b + 3) There is an order of precedence which determines which operators are done first: First *, /, and % are performed, and then + and -. Two arithmetic operators of the same precedence are done from left to right. Parentheses can be used to change the order. Arithmetic Expressions

  12. There are also operators which compare values and return true or false (actually 1 or 0, but think of them as true and false). For example “a < b” returns true if a is less than b and false otherwise. These are called relational operators. The relational operators are == (equals), != (not equals), < (less than), > (greater than), <= (less than or equals), >= (greater than or equals). Relational Operators

  13. We can combine simple relational tests together into more complex expressions using Boolean operators. Boolean operators work on logical (or Boolean) values (true, false) and return true and false. The common operators are && (and), || (or), and ! (not). 0 <= a && a <= 10 // a is between 0 and 10 incl. Boolean Expressions

  14. 0 < a || 0 < b // a is greater than 0 // or b is greater than 0. !(a > 5) // a is not greater than 5. Note that 0 <= a <= 10 is not a valid expression. Boolean Expressions (cont'd)

  15. Truth Tables

  16. Order of Precedence

  17. Order of Precedence (cont'd)

  18. Order of Precedence (cont'd)

  19. The Boolean operators && and || are evaluated using short-circuit evaluation. That is, if you have the expression (a > b) && (b > c), and it turns out that a is not greater than b (so a > b is false), then there is no need to evaluate b > c (since the whole expression is going to be false anyway). Similarly, if you evaluate (a > b) || (b > c) and a is greater than b, then the expression is true, so b > c is not evaluated. Short-Circuit Evaluation

  20. An If-Else statements allows the program to execute one section of the program if a Boolean expression is true, and a different section, if that Boolean expression is false. This is called conditional evaluation and allows the program to do different things given different inputs. If-Else Statements

  21. if (a > b) cout << a << endl; else cout << b << endl; If a is greater than b, then then first line is executed and the value of a is printed out. Otherwise, the second line is executed and the value of b is printed. If-Else Example

  22. The syntax for an If-Else statement is: if <boolean expression> <stmt1> else <stmt2> If-Else Statement Syntax

  23. If you want to do more than one statement if an IF-else case, you can form a block of statements, or compound statement, by enclosing the list of statements in curly braces ({ }). The statements in side of the braces will be executed sequential, and the block counts as one statement. Compound Statements

  24. if (a > b) { c = a; cout << a << endl; } else { c = b; cout << b << endl; } Compound Statement Example

  25. Write a program that reads in two integers and then prints out the smaller of the two followed by the larger of the two. You will need to: Prompt the user to type in two ints Read in the two ints Compare the two ints, and then depending on which is larger, print them out in the right order. Exercise

More Related