by John
Kopp
Welcome to About.com's C++ tutorial. This lesson will teach
you about using basic input and output in C++ and introduce
the string class. Usually i/o, input and output,
form an important part of any program. To do anything useful
your program needs to be able to accept input data and report
back your results. In C++, input and output are provided by
the iostream library. To use this library, add #include
<iostream> to the top of your program. This tells the
preprocessor to add code from the file iostream.h into your
source file. Including this file defines and initializes the
following objects for use in your program.
- cin - This object provides for input from the
terminal (keyboard)
- cout - This object provides for output to the
screen.
- cerr - This object provides unbuffered output
to the standard error device, which defaults to the screen.
Unbuffered means that any messages or data will be written
immediately. With buffered input, data is saved to a buffer
by the operating system, transparently to your program. When
the buffer is full, everything in it is written out. This is
more efficient because each write requires a certain amount
of overhead from the operating system. Writing out one large
buffer has less overhead than writing out multiple smaller
messages. The downside is that if a program crashes before
the buffer is written, nothing in the buffer is output.
Output via cerr is unbuffered to ensure that error messages
will be written out.
- clog - This object provides buffered output to
the standard error device, which defaults to the screen.
The best way to learn to use these objects is by example
and practice.
#include <iostream> #include
<string> using namespace std;
int
main() { string
name; int
ID;
cout << "Enter
your name "; cin >>
name;
cout <<
"Enter your ID number "; cin
>> ID;
cout
<< "Hello " << name << " or should I
say " << ID <<
endl;
return
0; }
|
C++ defines a string class as part of its standard
libraries. To use this class, you must include the "string"
include file in your source code. To use the string class, you
must declare a string object. The class defines what members it includes and what methods it has. Members are used to store the
data of the class. Methods provide the functionality of the
class, that is, what it can do. This will become clearer in
latter lessons. For now, use the string class as in the
examples. An object is an instance of a class. Just as ID
an object of data type "int", name is an object of class
"string". Try compiling and executing the above program for
practice. See my tutorials on compiling for help with
this.
The input operator, >>, is used
to put data into variables in your program. It is also called
the insertion operator. The , <<, is
used to direct output to standard output or standard error. It
is also called the extraction operator. These operators also
work with other streams including files. File i/o will be
covered in a latter lesson.
Practice
As I always emphasize, the
best way to learn is to actually write code. So, here are some
practice problems to work on.
1) Modify the example
program to input and output both first and last names.
Solution
2)
Extend the program to input and output other information.
Include age, occupation, salary, and address. What are data
types are appropriate for each of these new variables?
Solution
Next
page > Solution
to Problem 1 > Page 1,
2,
3