100 likes | 238 Views
Program Input and the Software Design Process. Robert REaves. Input Stream. The process of placing values from an outside data set into variables in a program is called input . Computer reads data into variables. This is from the standard input device , the keyboard.
E N D
Program Input and the Software Design Process Robert REaves
Input Stream • The process of placing values from an outside data set into variables in a program is called input. • Computer reads data into variables. • This is from the standard input device, the keyboard. • NOTE: We will look at file input later.
Input Stream • Think of the input stream as a doorway through which characters come into your program from an input device. • Include inside of the iostreamheader file. • #include<iostream> • cinis the stream that is associated with the standard input device. • cin >> name; • cinuses the extraction operator (>>), sometimes pronounced “get from”. • (>>) takes two operands. • Left hand: stream expression (so, this means cin) • Right hand: is a variable we store the input data in.
Input Stream • We can use the (>>) operator several times in a single input statement. • cin >> length >> width; • SAME AS: • cin >> length; • cin >> width; • The (>>) will skip leading whitespace or newlines, but terminate when reads one upon reading input. • You may confuse the (>>) operator and the (<<) operator. Just remember to use the operator that points in the direction which the data is going!
Reading Marker and the Newline Character • Reading Marker keeps track of the point in the input stream where the computer should continue reading. • This can be thought of kind of like a bookmark on your input. =) • The (>>) operator leaves the reading marker on the character following the last piece of data read. • Where does the newline character come from and what is it?
Get Function • We said that the (>>) operator skips whitespace, but what happens if we want an input to be some kind of whitespace? • With the (>>) operator that isn’t possible. • We can use the get function! • Gets the very next character in the input stream without skipping any whitespace characters. • cin.get(ch); • This is associated with the cin, or istream data type. So you must use the dot notation to make this function call.
Ignore Function • Isn’t used very much, but something you will love when you have to use it! • Ignore function is used to skip, read and discard, characters in the input stream. • cin.ignore(200, ‘\n’); • First argument to .ignore() is an int expression and the second being a char value. • int expression: amount of input characters to skip unless char value is found. • char value: skip int expression characters or until this is found.
Reading String Data • How to input a character string into a string variable. Two options. • We can use our (>>) • Stops when it reads whitespace. • string firstname; • string lastname; • cin >> firstname >> lastname; • This is a widely used for input purposes, but it has a drawback. • Cannot be used to input a string with blanks in it.
Getline Function • Getlinefunction does not skip leading whitespace characters and continues until it teaches the newline character. • Doesn’t use dot notation, requires two arguments. • First being your istream or cin in this case, second being your string variable. • getline(cin, string); • Reads and stores the entire input line, embedded blanks and all. • NOTE: newline character is consumed, but not stored into your string.
File Input and Output • Ifstream and ofstream are included inside the fstreamheader file. • #include <fstream> • We have learned how to use each one of these with cout and cin. We can now define our own input and output file streams using the ifstreamand ofstreamtype!