Lecture Programming in C++ - Chapter 4: Basic input and output. On completion of this chapter students will learned how to: Read input from keyboard, write output to file, read data from file, read and work with character data, work with the input buffer.
Chapter 4 – Basic Input and Output Reading Data Keyboard input – cin object – >> called extraction operator Syntax: cin >> variable; Example: cin >> income >> expense; Reads to values typed at keyboard (separated by whitespace) to memory locations names, income, and expense Lesson 4.1 Prompt Message sent from program to screen Informs user what data to enter Precedes cin statement in program int age; cout s2 >> s3; infile >> s4; s1 35 s2 12 s3 s4 3.5 Note: The number of data entries and their data types of variables should read should match the number and order within the file! Lesson 4.3 Reading Character Data Can use cin cin >> c1 >> c2 >> c3; Reads next three characters typed at keyboard Whitespace is NOT needed for separating character data – if used, whitespace is ignored Lesson 4.4 Examples: char c1,c2,c3,c4,c5,c6,c7,c8; cin >> c1 >> c2 >> c3; cin >> c4 >> c5; cin >> c6; cin >> c7 >> c8; c1 a c5 c2 f c6 h c3 d c7 j c4 c8 w Lesson 4.4 Keyboard Input af d32(enter) hj(tab)w (enter) k Note: All whitespace characters are ignored! Input Buffer Region of memory used for temporary storage of information being transferred between devices Accessed sequentially Position indicator keeps track of point to which information has been read Lesson 4.4 Input Buffer Keyboard entry: af d32(enter) hj(tab)w (enter) a f d 3 2 (enter) h j (tab) w (enter) cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6; a f d h Lesson 4.4 Summary Learned how to: Read input from keyboard Write output to file Read data from file Read and work with character data Work with the input buffer ... cout