Inputoutput
Inputoutput
Inputoutput
Standard input and output in C++ is done through the use of streams. Streams are
generic places to send or receive data. Keyboard, screen, file, network, it's all the
same after setup. It's a stream.
There are two variables (among others) defined in <iostream>. cout is used for
output, cin for input.
Important Point
cout and cin are not key words in the C++ language. They are variables,
instances of classes, that have been declared in <iostream>. cout is a variable
of type ostream. cin is a variable of type istream.
« is sometimes called the output operator, sometimes the insertion operator (it inserts
something into the stream)
We don't need to tell the type of the data to be output, that happens automatically
(we'll find out how later)
int i = 7;
double d = 3.4;
cout << i;
cout << d;
1
cin >> i;
cin >> d;
Note that address operators (&) are not needed here like they are with the standard C
input function scanf(). (And there was much rejoicing.)
The cin input stream can be chained as well, but usually isn't.
Note that output goes to cout, input comes from cin. It may be possible to get input
from cout or send output to cin depending on the library implementation, but it
shouldn't do anything useful.
To get an entire line from cin, there exists a function, called getline, that takes the
stream (cin) as first argument, and the string variable as second. For example:
int main ()
{
string mystr;
cout << "What's your name? ";
getline (cin, mystr);
cout << "Hello " << mystr << ".\n";
cout << "What is your favorite team? ";
getline (cin, mystr);
cout << "I like " << mystr << " too!\n";
return 0;
}
Note:
string name;
cout << "Enter your name: " << flush;
cin >> name;
// read string until the next separator
// (space, newline, tab)
2
getchar() - This function reads in a character. It returns the character as the ASCII
value of that character. This function will wait for a key to be pressed before
continuing with the program.
int main()
{
char a_char;
cout<<"Enter y or n: ";
a_char=getchar();
cout<<a_char;
}
Output Formatting
Formatting in the standard C++ libraries is done through the use of manipulators,
special variables or objects that are placed on the output stream. Most of the standard
manipulators are found in <iostream> and so are included automatically. The standard
C++ manipulators are not keywords in the language, just like cin and cout, but it is
often convenient to think of them as a permanent part of the language.
int main()
{
3
cout << "Hello world 1" << endl;
cout << "Hello world 2\n";
return 0;
}
produces
Hello world 1
Hello world 2
flush
Important Point
The setw() manipulator takes an integer argument which is the minimum field
width for the value to be printed.
Important Point
All manipulators that take arguments are defined in the header file iomanip. This
header file must be included to use such manipulators.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// A test of setw()
cout << "*" << "Hi there!" << "*" << endl;
cout << "*" << setw(20) << "Hi there!" << "*" << endl;
cout << "*" << setw(3) << "Hi there!" << "*" << endl;
4
return 0;
}
produces
*-17*
* -17*
*Hi there!*
* Hi there!*
*Hi there!*
Note that the values are right justified in their fields. This can be changed. Note
also what happens if the value is too big to fit in the field.
Important Point
The argument given to setw() is a minimum width. If the value needs more
space, the output routines will use as much as is needed.
This field overflow strategy is different than that used in other programming
languages. Some languages will fill a small field with *'s or #'s. Others will
truncate the value. The philosophy for the C++ standard output is that it's better
to have a correct value formatted poorly than to have a nicely formatted error.
Important Point
Justification
Values can be justified in their fields. There are three manipulators for
adjusting the justification: left, right, and internal.
Important Point
Important Point
All manipulators except setw() are persistent. Their effect continues until
explicitly changed.
5
left
- right justify all values in their fields. This is the default justification value.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "*" << -17 << "*" << endl;
cout << "*" << setw(6) << -17 << "*" << endl;
cout << left;
cout << "*" << setw(6) << -17 << "*" << endl << endl;
cout << "*" << "Hi there!" << "*" << endl;
cout << "*" << setw(20) << "Hi there!" << "*" << endl;
cout << right;
cout << "*" << setw(20) << "Hi there!" << "*" << endl;
return 0;
}
produces
*-17*
* -17*
*-17 *
*Hi there!*
*Hi there! *
* Hi there!*
internal
using std::cout;
using std::endl;
using std::setw;
using std::internal;
int main()
{
cout << setw(9) << -3.25 << endl;
cout << internal << setw(9) << -3.25 << endl;
6
return 0;
}
produces
-3.25
- 3.25
showpos and noshowpos
This manipulator determines how positive numbers are printed. A negative
number is traditionally printed with a minus sign in front. The lack of a minus
sign means a positive value. However some accounting and scientific
applications traditionally place a plus sign in front of positive numbers just to
emphasize the fact that the number is positive. Using the showpos manipulator
makes this happen automatically. The noshowpos manipulator returns the
output state to placing nothing in front of positive values.
#include <iostream>
using namespace std;
int main()
{
int pos_int = 4, neg_int = -2, zero_int = 0;
float pos_f = 3.5, neg_f = -31.2, zero_f = 0.0;
cout << "pos_int: " << pos_int << " neg_int: " << neg_int;
cout << " zero_int: " << zero_int << endl;
cout << "pos_f: " << pos_f << " neg_f: " << neg_f;
cout << " zero_f: " << zero_f << endl << endl;
cout << "pos_int: " << pos_int << " neg_int: " << neg_int;
cout << " zero_int: " << zero_int << endl;
cout << "pos_f: " << pos_f << " neg_f: " << neg_f;
cout << " zero_f: " << zero_f << endl << endl;
return 0;
}
produces
pos_int: 4 neg_int: -2 zero_int: 0
pos_f: 3.5 neg_f: -31.2 zero_f: 0
7
There are 3 floating point formats: general, fixed, and scientific. Fixed format
always has a number, decimal point, and fraction part, no matter how big the
number gets, i.e., not scientific notation. 6.02e+17 would be displayed
as 602000000000000000 instead of 6.02e+17.
General format is a mix of fixed and scientific formats. If the number is small
enough, fixed format is used. If the number gets too large, the output switches
over to scientific format. General format is the default format for floating point
values.
The manipulator fixed will set up the output stream for displaying floating
point values in fixed format.
int main()
{
float small = 3.1415926535897932384626;
float large = 6.0234567e17;
float whole = 2.000000000;
8
cout << "whole: " << whole << endl << endl;
cout.unsetf(ios::fixed | ios::scientific);
return 0;
}
produces
Some values in general format
small: 3.14159
large: 6.02346e+17
whole: 2
whole: 2
setprecision()
9
In general format, the precision is the maximum number of digits displayed.
This includes digits before and after the decimal point, but does not include the
decimal point itself. Digits in a scientific exponent are not included.
In fixed and scientific formats, the precision is the number of digits after the
decimal point.
Important Point
The setprecision manipulator allows you to set the precision used for printing
out floating point values. The manipulator takes an integer argument. The
header file <iomanip> must be included to use this manipulator.
The showpoint manipulator forces trailing zeros to be printed, even though they
are not needed. By default this option is off. As can be seen from previous
examples, this manipulator is not needed in fixed or scientific format, only in
general format.
#include <iostream>
using std::cout;
using std::endl;
using std::showpoint;
int main()
{
float lots = 3.1415926535;
float little1 = 2.25;
float little2 = 1.5;
float whole = 4.00000;
cout << "Some values with noshowpoint (the default)" << endl << endl;
10
cout << "little1: " << little1 << endl;
cout << "little2: " << little2 << endl;
cout << "whole: " << whole << endl;
cout << "The same values with showpoint" << endl << endl;
return 0;
}
produces
Some values with noshowpoint (the default)
lots: 3.14159
little1: 2.25
little2: 1.5
whole: 4
lots: 3.14159
little1: 2.25000
little2: 1.50000
whole: 4.00000
11