Download presentation
Presentation is loading. Please wait.
Published byEarl Reed Modified over 8 years ago
1
Math Operators and Output Formatting
2
Incrementing and Decrementing StatementEquivalent Counter++;Counter = Counter + 1; ++Counter;Counter = Counter + 1; Counter--;Counter = Counter – 1; --Counter;Counter = Counter – 1;
3
KJ 10 K = J++;// post-fix K 10 J 11 KJ 10 K 11 J If J holds a value of 10 Then, K = ++J;// pre-fix
4
Assignment Operator i = 25; More Assignment Operator
5
Dividing by Zero In mathematics, division by zero is infinitive. In Computer Science, it generate an error. Overflow Overflow is the condition when a value becomes too large for its data type. Underflow Underflow is the condition when a number is too small for the data type Precision When a number’s significant decimal place is important to the accuracy of the calculation, it is necessary to promote to the memory of another data type. For instance, use double instead of float to represent your number
6
Mixing Data Type C++ allow you to mix data types in calculation. In case of mixed data types, the compiler makes adjustments so as to produce the most accurate answer. For example, the integer value (number_of_people) is temporarily converted to a float so that the fractional part of the variable (money) can be used in the calculation. The is called promotion. int number_of_people; float money, share; share = money / number_of_people;
7
Implicit Promotion 5/2yield2 2/4yield 0 5/2.0 or 5.0/2.0 or 5.0/2yield 2.5 float a=10, b=5, x; int m=3.0, n=2.5, y; y = m/n;yield1 x = m/n;yield1.0 x = 5.5; y = x + m/n; ==> y = x + 1 ==> y = 5.5 + 1.0 ==> y = 6.5 ==> y = 6
8
Typecasting C++ allows you to explicity change one data type to another using operators called typecast operators using cast operator to create a temporary copy of the variable. float payRate = (float) 0.0; int Num; Num = (int) payRate;
9
Input Skip all the leading delimiter ( space, tab, ) Char read 1 at a time (1 non blank) Data will be read until space >> extraction operator Form: cin >> dataVariable; cin >> age >> firstInitial;
10
Output Output stream cout << Output operator (insertion operator) –cout << “my height in inches is: “ << height; Blank lines –endl; or “\n”; Form:cout << dataVariable;
11
Using setf and unsetf to format output The cout object has format options that can be changed. To change these option, you send a message to the object using serf and unsetf. OptionDescription LeftLeft-justifies the output RightRight-justifies the output ShowpointDisplays decimal point and trailing zeros for all floating-point numbers, even if the decimal places are not needed. UppercaseDisplays the “e” in E-notation as “E” ShowposDisplays a leading plus sign before positive values ScientificDisplays floating-point number in scientific (“E”) notation fixedDisplays floating-point numbers in normal notation Examples are illustrated in knowlton p94
12
//coutsetf.cpp - Knowlton p 99 #include using namespace std; int main() { float x = 24.0; cout << x << endl;// display 24 cout.setf(ios::showpoint); cout << x << endl;// display 24.0000 cout.setf(ios::showpos); cout << x << endl;// display +24.0000 cout.setf(ios::scientific); cout << x << endl;// display +2.400000e+001 cout.setf(ios::uppercase); cout << x << endl;// display +2.400000E+001 cout.unsetf(ios::showpoint); cout << x << endl;// display +2.400000E+001 cout.unsetf(ios::showpos); cout << x << endl;// display 2.400000E+001 cout.unsetf(ios::uppercase); cout << x << endl;// display 2.400000e+001 cout.unsetf(ios::scientific); cout << x << endl;// display 24 return 0; }
13
I/O Manipulators It required to include a library in the section for compiler directives. setprecision - to set the number of digits that are to appear to the right of the decimal point. #include cout << setprecision(2) << price << ‘\n’; setw - to set the minimum number of spaces for the number to be display on output. The amount of space used to display a number is called the field width. setw can only the format for the next output. cout << setw(2) <<‘$’ << total << “\n\n”;
14
//This program demonstrates the setw manipulator being //used with values of various data types. #include using namespace std; int main() {int IntValue = 3928; float FloatValue = 91.5; char StringValue[14] = "John J. Smith"; cout << "(" << setw(5) << IntValue << ")" << endl; cout << "(" << setw(8) << FloatValue << ")" << endl; cout << "(" << setw(16) << StringValue << ")" << endl; return 0; } Program Output ( 3928) ( 91.5) ( John J. Smith)
15
getline - could be used to read in a string including space getline does not skip leading white space string name; getline (cin, name); ignore - skips up to n characters in the specified stream (cin) until the character argument (delimiter) is encountered. cin.ignore(100,’\n’); cin.ignore(80,’*’);
16
// instring.cpp #include using namespace std; int main() { string FirstName; string LastName; cout << "Enter your first name: "; getline(cin, FirstName); cout << "Enter your last name: "; getline(cin, LastName); cout << "Your name is " << FirstName << " " << LastName << ".\n"; return 0; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.