CPSC 490 Input and Output in C++: #Include #Include Using Namespace STD
CPSC 490 Input and Output in C++: #Include #Include Using Namespace STD
Introduction
Data input/output in C++ is done via stream classes defined in the Standard
Template Library (STL). The <iostream> library provides all the functionalities for
you to read from standard input and write to standard output. In this course, all I/O
will be done via standard input and standard output, so it is wise to use the
<iostream> library in every program that you will be writing. In addition, the
<sstream> library deals with I/O using the STLs string class. This library is useful in
processing a string with a variety of data. To use these features, you should start
any programs with the following header:
#include <iostream>
#include <sstream>
using namespace std;
Note that <sstream> automatically includes the <string> class. Also, identifying the
namespace std is important, or you will need to prefix all standard library class,
methods, and variables with a std::.
Standard Input
<iostream> has the global variable cin, which defines the standard input stream.
The most common use of cin is its extraction operator >>. This operator is
overloaded for all standard types, including int, double, and string.
int n;
double f;
string s;
cin >> n >> f >> s;
Example 1. Reading an integer, then a double, then a string from standard input.
The extraction operator also does some input processing. Using this operator, the
input stream will read and discard any whitespaces, then parse data until it reaches
the next whitespace. For example, when the program in Example 1 is fed the
following 3 input files, the results are the same:
10 10____ ____10______4.5__________
4.5_abc ____4.5 __abc
^ abc____ ^
^
Example 2. Three different input files where Examle 1s program will read the same
information for n, f, and s. Spaces are replaced with _ for clarity.
This is a very useful property of C++s streams. You do not have to worry about
spaces separating input data. On the other hand, this can be troublesome in some
instances. For example, suppose you want to read in a persons name:
string name;
cin >> name;
In this program, if the input has embedded whitespace, as in John Doe, then the
input will only set name=John, ignoring the last name. To fix this problem, we
need to read the input line by line.
CPSC 490 Input and Output in C++ Page 2 of 3
To read input line by line, C++ provides the global getline function call.
This getline function reads from the input stream is, which in our case will be cin,
until one of the followings occurs:
(a) The end-of-file is reached,
(b) The maximum number of characters to be fit into a string is read,
(c) The delimiter character delim is read.
string names[100]; John Doe
int i = 0; Mary Martin
while ( getline( cin, names[i] ) ) { .
i++; .
} .
Example 3. Reads from standard input line by line, storing each line in the names array.
Note: The getline function call will read in the delimiter and discard it. So in the
example above, names[0]=John Doe, and not John Doe\n.
Standard Output
The standard output stream in C++ is cout. In parallel to cins extraction operator,
output streams in C++ define the insertion operator <<. Similarly, the insertion
operator is overloaded for all standard data types, and can be used in sequences:
The special variable endl is defined in <iostream>, standing for end-line. All it
does is to print a \n character, then flush the output stream. This is helpful when
printing to a console, where the output you are waiting for should be printed
immediately, and not buffered.
The code above reads from standard input line by line, where each line starts with
the persons age, followed the persons full name. Line 10 defines the stringstream
object strin. The constructor takes one parameter: the string to be processed. Line
11 uses the extraction operator to read the age of the person. Line 12 will then
process the rest of the line using getline.
Question: A small bug exists in the above code. Can you find it?
You can also use stringstream to write to strings, then use the str() member function
to get the string of the current stream. The following is an easy way to convert any
number to a string.
This creates an empty stream strout, writes an integer to it using the overloaded
extraction operator, and returns the string written using str().