160 likes | 308 Views
ECE 264 Object-Oriented Software Development. Instructor: Dr. Honggang Wang Fall 2012 Lecture 5: Continuing with C++ I/O Basics. Lecture outline. Announcements/reminders Lab 1; due Monday. 09/17 TA: Justin R Lacle ( jlacle@umassd.edu )
E N D
ECE 264Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 5: Continuing with C++ I/O Basics
Lecture outline • Announcements/reminders • Lab 1; due Monday. 09/17 • TA: Justin R Lacle (jlacle@umassd.edu) • TA Office hour: 10-11:00 am, Tuesday and 11:00-12:00 pm, Thursday • Class folder now set up on M:\ drive • Should have folder under M:\ECE-264\<username> • Continue with C++ basics • Review basic I/O • Work through examples ECE 264: Lecture 4
Review • Last lecture—examples • Output (cout) streams • Can output multiple values in same statement • cout << “x=“ << x << “, y=“ << y << endl; • Input (cin) streams • Use cin to read values into variables • E.g., cin >> x; • Skips whitespace characters • Can cause problems if input doesn’t match variable type ECE 264: Lecture 4
//Example 2: Determine the output #include <iostream> using std::cout; using std::cin; using std::endl; int main() { int i, j; double x, y; cin >> i >> j >> x >> y; cout << "First output " << endl; cout << i << ',' << j << ',' << x << ',' << y << endl; cin >> x >> y >> i >> j; cout << "Second output" << endl; cout << i << ',' << j << ',' << x << ',' << y << endl; return 0; } //Input stream is: 1 2 3.4 5 2 3 3.4 7 First output 1,2,3.4,5 Second output 3,2,2,3 _ ECE 264: Lecture 4
Example 2 (cont.) • Issues with second output • Before highlighted lines i = 1, j = 2, x = 3.4, y = 5 • Second cin appears to assign: x = 2, y = 3, i = 3.4, j = 7 • But printing i, j, x, and y in order yields: 3,2,2,3 • j never gets value 7—what happens? • i and j are integers • cin reads 3 into i and then stops at decimal point • cin can’t skip non-whitespace character j doesn’t change ECE 264: Lecture 4
Characters and input • >> discards leading whitespace • get() method used to input whitespace characters • Optional second argument allows you to input multiple characters • Default is 1 • cin.get(buffer, 10) reads 10 characters from input • Example: int x; char ch; cin >> x >> ch; cin >> x; cin.get(ch);x ch x ch 45 ‘c’ Input stream: 45 c 39 b 39 ‘\n ’ ECE 264: Lecture 4
Characters and input (cont.) • Reading an entire line: getline(char[], num) • Reads up to num characters on a line • Stops at newline character • Example: cin.getline(buffer, 10); • Must be careful if input is read using stream extraction operator ( >> ) as well as getline() ECE 264: Lecture 4
getline example int numR; char name[20]; cin >> numR; cin.getline(name, 20); • If input is: 6 Room 12 what values do numR and name hold? • numR = 6 • name = “\n” why? • cin >> numR stops at any whitespace character \n • cin.getline(name,20) starts with next char, ends at newline ECE 264: Lecture 4
Fixing getline example • Skipping whitespace characters: ignore(num) • Discards num characters from input stream without storing them • To fix previous example: int numR; char name[20]; cin >> numR; cin.ignore(1); cin.getline(name, 20); ECE 264: Lecture 4
Formatted output • Recall earlier example: int i, j; double x, y; ... cin >> x >> y >> i >> j; cout << "Second output" << endl; cout << i << ',' << j << ',' << x << ',' << y << endl; • x and y are of type double … • ... but second cout prints 2 & 3 for x & y • What if we want to • Always print decimal point? • Always show certain number of places after point? • Force output to align within columns of a particular width? • Change the number base being printed? • Use stream manipulators: objects affecting output stream • Already seen one of these: endl • To use others, must add #include <iomanip> • May also use stream functions: functions associated with cin/cout ECE 264: Lecture 4
Integral Stream Base: dec, oct, hex and setbase • Change a stream’s integer base by inserting manipulators • hex manipulator • Sets the base to hexadecimal (base 16) • oct manipulator • Sets the base to octal (base 8) • dec manipulator • Resets the base to decimal • setbase parameterized stream manipulator • Takes one integer argument: 10, 8 or 16 • Sets the base to decimal, octal or hexadecimal • Requires the inclusion of the <iomanip>header file • Stream base values are sticky • Remain until explicitly changed to another base value ECE 264: Lecture 4
Example 1: bases // Fig. 15.8: Fig15_08.cpp // Using stream manipulators hex, oct, dec and setbase. #include <iostream> using std::cin; using std::cout; using std::dec; using std::endl; using std::hex; using std::oct; #include <iomanip> using std::setbase; ECE 264: Lecture 4
Example 1: bases (cont.) int main() { int number; cout << "Enter a decimal number: "; cin >> number; // input number // use hex stream manipulator to show hexadecimal number cout << number << " in hexadecimal is: " << hex << number << endl; // use oct stream manipulator to show octal number cout << dec << number << " in octal is: " << oct << number << endl; // use setbase stream manipulator to show decimal number cout << setbase( 10 ) << number << " in decimal is: " << number << endl; return 0; } // end main ECE 264: Lecture 4
Example 2: bases //Example: Determine the output #include <iostream> using std::cout; using std::endl; using std::hex; using std::oct; int main() { int i; for (i = 0; i <= 32; i += 8) { cout << "Decimal: " << i << endl; cout << oct << "Octal: " << i << endl; if ((i % 16) == 0) cout << hex << "Hexadecimal: " << i << endl; } return 0; } ECE 264: Lecture 4
Example 2: bases (cont.) • Output: Decimal: 0 Octal: 0 Hexadecimal: 0 Decimal: 8 Octal: 10 Decimal: 20 Octal: 20 Hexadecimal: 10 Decimal: 18 Octal: 30 Decimal: 40 Octal: 40 Hexadecimal: 20 • What’s the problem? • Base settings are sticky • How would we fix it? • Insert “dec” into the cout statement for the decimal values ECE 264: Lecture 4
Final notes • Next time • More on formatted output • Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: • Deitel & Deitel, C++ How to Program, 8th ed. • Etter & Ingber, Engineering Problem Solving with C++, 2nd ed. ECE 264: Lecture 4