Input and Predefined Functions
Chapter 3
I/O: sequence of bytes (stream of bytes) from source to destination
- Bytes are usually characters unless the program requires other types of information
- Stream: sequence of characters (or bytes) from source to destination
- Input stream: sequence of characters (or bytes) from an input device to the computer
- Output stream: sequence of characters (or bytes) from the computer to an output device
Use iostream
header file to receive data from the keyboard and send output to the screen.
- Contains definitions of two data types (variable types like
int
orstring
):istream
: input streamostream
: output stream
- Has two variables:
cin
: stands for common inputcout
: stands for common output
- Variable declaration is like:
istream cin;
ostream cout;
- To use
cin
andcout
, the preprocessor directive#include <iostream>
must be added to the top.
Input
cin
and the extraction operator, >>
>>
is a binary operator- When scanning,
>>
skips all whitespace.- Blanks and certain nonprintable characters
>>
distinguishes between character 2 and number 2 by the right-side operand of>>
.- If type
char
orint
(ordouble
), the 2 is treated as a character or as a number 2.
Data Type | Valid Input |
---|---|
char | One printable character except the blank |
int | An integer (whole number), possibly preceded by a + or – sign |
double | A decimal number, possibly preceded by a + or – sign. If the actual data input is an integer, the input is converted to a decimal number with the decimal part equal to 0 . |
Exploring User Input and Input Errors
Try the following code snippet from the video.
Try the following code snippets with:
- letter
- word
- positive number
- negative number
- multi-digit number
- decimal number
- combination (with and without spaces)
/*
* Test program to see what happens with the user input does not match the
* variables' types
*
* by Sean T. Hayes
*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Variables to receive input
char ch1 = ' '; // 1 character
char ch2 = ' '; // a second character
int int1 = -8888; // whole number
double doub1 = -999.999; // decimal (floating point) number
string text;
// Get input from the user
cout << "Input a char, floating-point number, char, int, string: ";
cin >> ch1 >> doub1 >> ch2 >> int1;
getline(cin, text);
// Display the values for each variable
cout << endl;
cout << " Double: " << doub1 << '\n'
<< "Characters: '" << ch1 << "', '" << ch2 << "'\n"
<< " Integer: " << int1 << endl
<< " String: \"" << text << "\"" << endl;
return 0; // exit with success code.
}
If the user inputs a character or something erroneous like that – say, “Hello there” for an integer, the system enters what is known as a “fail state”. The integer is not set to anything, and the computer no longer takes in any other input.
Predefined Functions
- Function (subprogram): set of instructions
- When activated, it accomplishes a task
- main executes when a program is run
- Other functions execute only when called
- C++ includes a wealth of functions
- Predefined functions are organized as a collection of libraries accessible through header files.
To use pow()
(which is short for power), include cmath.
- Two numeric parameters
- Syntax:
pow(x, y)
meansand are the arguments or parameters
- In
pow(2, 3)
, the parameters are2
and3
Input including Whitespace
The cin.get()
function inputs the next character (including whitespace).
- Stores in memory location indicated by its argument
cin.get(varChar)
;varChar
is achar
variable- is an argument (or parameter) of the function
cin
andget
are two separate identifiers separated by a dot- The dot separates the input stream variable name from the member (e.g., function name).
- In C++, dot is the member-access operator.
The getline()
function gets a whole line of user input including spaces as one string variable.
string userIntput;
getline(cin, userInput); // read until a newline character.
In the previous code, if the user inputs: Hello world !!!
, then userInput
will contain, "Hello world !!!"
.
Adding a third parameter changes getline()
to stop at that character instead of a newline ('\n'
). For example,
string userIntput;
getline(cin, userInput, '$'); // read until a $ character.
Ignoring Characters
The cin.ignore()
function discards a portion of the input.
cin.ignore(intExp, chExp);
- Ignore the next
intExp
characters unless we reach achExp
. - Often
'\n'
is a great value to pass to the second parameter to ignore everything on the current line of input. - Examples:
ignore(); // ignore the next character.
ignore(100); // ignore the next 100 characters.
ignore(INT_MAX, '\n'); // ignore until newline.
INT_MAX
is a constant defined in theclimits
header that holds the maximum value for an integer.
Keeping Values within the Input Stream
The cin.putback()
function places the previous character extracted by the get function from an input stream back to that stream.
The cin.peek()
function returns the next character from the input stream.
- It does not remove the character from that stream.
Handling Input Errors
The cin.clear()
function restores the input stream to a working state. Useful after an input error.
- It does not remove the current contents of the stream. For that,
ignore()
withclear()
.
Self-Check Questions
- What is a stream of data?
- How would you read a string of data with spaces?