show_text_file_io

show_text_file_io - CHAPTER 4 Programming with cin , cout ,...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CHAPTER 4 Programming with cin , cout , and Data Files March 28, 2008 OOP Object Oriented Programming class A class is a mechanism that allows a programmer to define new data types. A class can be used to add functionality to an existing data type or to create a new data type. A class definition combines data and functionality. object An object is a variable of a defined class type, also referred to as an instance of a class . OOP Define classes, Use instances... cin & cout as Instances cin is an input stream cin object of type istream istream class is defined in file iostream #include <iostream> eof() is a member function of cin . 1 i n t N ( 0 ) ; / / number o f d a t a p o i n t s 2 d o u b l e sum ( 0 ) , r e a d _ s p a c e ; 3 4 wh i l e ( t r u e ) { 5 / / rea d f l o a t i n g p o i n t 6 c i n >> r e a d _ s p a c e ; 7 i f ( c i n . e o f ( ) ) { 8 b r eak ; 9 } 10 sum += r e a d _ s p a c e ; 11 N ++; 12 } 13 14 c o u t . s e t f ( i o s : : f i x e d ) ; 15 c o u t . p r e c i s i o n ( 3 ) ; 16 c o u t << "Average: " \ 17 << sum / N << e n d l ; OOP Define classes, Use instances... cin & cout as Instances cout is an output stream cout object of type ostream ostream class is defined in file iostream #include <iostream> More member functions ! cout.setf( ios::fixed ); cout.precision(3); 1 i n t N ( 0 ) ; / / number o f d a t a p o i n t s 2 d o u b l e sum ( 0 ) , r e a d _ s p a c e ; 3 4 wh i l e ( t r u e ) { 5 / / rea d f l o a t i n g p o i n t 6 c i n >> r e a d _ s p a c e ; 7 i f ( c i n . e o f ( ) ) { 8 b r eak ; 9 } 10 sum += r e a d _ s p a c e ; 11 N ++; 12 } 13 14 c o u t . s e t f ( i o s : : f i x e d ) ; 15 c o u t . p r e c i s i o n ( 3 ) ; 16 c o u t << "Average: " \ 17 << sum / N << e n d l ; Goals Learn how to reliably read input from cin . Learn how to format the output of cout . Leverage our familiarity of cout and cin to write and read from our own files (not just the keyboard and console). Reading from cin cin Properties cin ignores all leading whitespace or line endings before it parses real (non-whitespace) character data. once begun, cin stops parsing at any whitespace or the first nonsensical character for the type of value being read. nonsensical depends on where the character is encountered. Only the fourth plus symbol is nonsensical in this stream of integers: +1234 +123++4 Demo: Edit Program Run Program Reading from cin Reading Whole Lines Why cant we read whole lines in C++? Reading from cin Reading Whole Lines Why cant we read whole lines in C++? You can, by reading into a string variable type with the getline function: 1 / *** 2 * D i s p l a y numbered l i n e s o f c i n i n p u t 3 * / 4 s t r i n g i n p u t L i n e ; 5 i n t l i n eNo ( 1 ) ; 6 7 c o u t << "Enter lines of any length. " ; 8 c o u t << "CTRL-D or CTRL-Z to quit" << e n d l ; 9 10 / / The \ n r e p r e s e n t s t h e n e w l i n e...
View Full Document

Page1 / 32

show_text_file_io - CHAPTER 4 Programming with cin , cout ,...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online