<NOSCRIPT><A href="http://about.com/" target=_blank><IMG src="C++ Tutorial - Lesson 4 Input and Output - cin, cout, iostream_files/0.gif" border=0></A></NOSCRIPT>   About > Computing & Technology > C/C++ 
Search     
 Hi, I'm John Kopp, your guide to C, C++ and C# programming. This site provides what you need to know to learn C, C++ or C# programming. Browse the subjects on the left, try one of the tutorials or use the search box to get started.

C/C++

with John Kopp
Your Guide to one of hundreds of sites
 Home · Articles · Forums · Chat · Newsletters · Help    
Subjects

  ESSENTIALS
· C Tutorial
· C++ Tutorial
· C/C++ Glossary
· Tips
· More Tutorials
  BUYER'S GUIDE

Product Reviews
Top Picks
C Tutorials
C++ Tutorials
C#
Advanced C
Advanced C++
Beginning C
Beginning C++
Books
C++ Builder
Careers
CGI
CM
Dictionaries
CompilingDebugging
Freeware/Shareware
Humor
Magazines
OOAD
Polls
SoftwareEngineer'n
Style
STL
UNIX/GNU
Visual C++

Subject Library

All articles on this topic

 

Stay up-to-date!
Subscribe to our newsletter.

Web Hosting
Global Servers

 
 
 
Advertisement
> Free Credit Report
 
C++ Tutorial - Lesson 4: Input and Output
 Related Resources
• C++ Tutorial - See all lessons
• Compiling a Program from the Command Line
• Compiling a Program using Visual C++
• C++ Tutorial - Lesson 2: Variables
• C++ Programming Tips on Input
 
 From Other Guides
• Unix 101: Input/Output Redirection
• Advanced Unix Input/Output Redirection
 

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 output operator, <<, 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

Subscribe to the C/C++ Newsletter
Name
Email






Email this page | Sign up for a Newsletter |
Explore More
Most Popular Articles
• C++ Programming Tutorial
• C Programming Tutorial
• C++ Tutorial - Lesson 12: File Input and Output
• C++ Tutorial - Lesson 4: Input and Output - cin, cout, iostr...
• C++ Tutorial - Lesson 10: Arrays and Vectors
• C++ Tutorial - Lesson 1: Writing and Compiling A First Progr...
• Tutorials
What's Hot Now
• Teach Yourself C++ in 21 Days by Jesse Liberty
• C Programming Tutorial
• C++ Tutorial - Lesson 25: Operator Overloading
• The C Programming Language, 2nd Edition by Kernighan and Rit...
• Topics in C++: Function Template Specialization
• C Programming Tips: Using Long Constants
• C Tutorial - Lesson 5: Conditional Processing, Part 1 - Rela...
About Us | Advertise on This Site | User Agreement | Privacy Policy | Kids' Privacy Policy | Help
Copyright  © 2004 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.