Console Input and Output
Input-Output Stream:
a sequence of characters from an input device to the computer, or from the
computer to an output device.
General form of the cin statement: cin >> variable [ >> variable ] ...;
The extraction operator (>>) skips all whitespace* characters, then
extracts the next value corresponding to the variable's data type;
whereas the insertion operator (>>) places data from memory into the output
stream. (* blank, tab, return code)
Preprocessor directive required for cin and cout: #include <iostream>
Namespace statement for direct reference to cin & cout: using namespace std;
1. Given a type double variable named gasPrice, code a cin statement to input
and extract a value for storing (sample input: 3.35).
2. Suppose you drive some distance in whole miles and use a certain amount
of gasoline (could have a fractional portion). Define variables for the
miles driven and for gasoline used. Then code a cin statement to input
both milesDriven and gasUsed.
General form of the cout statement:
cout << expression or manipulator [ << expression or manipulator ] ...;
3. Code cout statements for use as prompts to input gasPrice, milesDriven and
gasUsed prior to the cin statements for questions 1 and 2.
4. Code cout statement(s) to print the string "MPG = " as a side label prior
to an expression of milesDriven divided by gasUsed.
Input-Output and the string type:
Preprocessor directive: #include <string>
may be needed when processing variables of the data type string
Example: given an input stream containing Commerce Texas, for the statement:
cin >> city;
only Commerce stored because extraction terminates when whitespace sensed.
[note: a cout statement will print an entire string containing whitespace]
5. Given that a string named state has been declared, code cin statement(s)
to input and store Commerce as city and Texas as state.
General form of the getline function: getline(istreamVar, strVar);
This function reads until it reaches the end of the current line;
Example: getline(cin, city);
(With "Fort Worth" in the stream, both words would be stored in city.)
General form of cin.get: cin.get([varChar])
This inputs the next character without skipping whitespace.
6. Code a string variable named courseTitle, a prompt, then a getline()
statement to extract the words "Introduction to Computer Science" from the
input stream to assign to courseTitle. "Echo" courseTitle.