Input/output in C++

This page introduces you to a very small number of simple techniques for input and output. These techniques will suffice throughout this module.

Warning.

Input/output is a huge subject and difficult to do well. There are a vast number of library functions to learn and many different techniques. It would be easy to spend three times the length of this module just on input and output. So I won't. But if you are doing programming work beyond this module and need some specific kinds of I/O please read manuals, web pages, etc., and use good library functions written by others, rather than wasting your time on writing your own.

We have already seen output to cout. The "console" cout is designed in such a way that for almost all objects X of any type, the line cout << X prints X to the console. In fact cout << X is an expression, so returns a value. This value is always cout itself, so you can place other things on that! This is why the multiple output expression cout << X << Y << Z also works. (A semicolon is also needed to terminate this statement, I have omitted it here because strictly speaking it is not actually part of the expression here.)

One way to handle input (which is a rather more tricky topic because the data to be input is usually written by a human and therefore could have errors) is to use "console input", which is defined in <iostream> as cin. Roughly, the same thing works except that >> is "get from", so cin >> X; gets a value for X from the console.

To use this requires variables so this web page is also the briefest introduction to variables. For more on this important topic see other web pages. For now what you need to know is that a variable is a box containing data. cin >> X; gets that value for X from the console. Of course the kind of data that can be stored in X is important too. For this web page that can be a string, an int (a kind of C++ integer) or a double (a kind of C++ real number). The compiler must be told which it is, and this is called declaring the variable.

If you use strings in your program you need to put #include <string> somewhere near the top.

These ideas are illustrated by the following programs.

// helloname.cpp (click here to download)

// This program illustrates cin and string
#include <iostream>
#include <string>

using namespace std;

int main() {
  string username;
  cout << "What is your name?" << endl;
  cin >> username;
  cout << "Hello " << username << "!" << endl;
  return 0;
}

Here the variable username is declared to have type string and console input is used to give it a value.

Exercise.

Try the program out and see if you can find any limitations of console input. In particular what happens when you enter your first name? your full name?

An alternative function to read a whole line is getline. You write getline(cin,stringvariable); with semicolon as shown, and the function gets a whole line and places that value (as a string) in stringvariable.

Exercise.

Try using getline to get the computer to write "Hello <your full name>!".

The next program takes an int value from the input. Try it out. Try to test it thoroughly. What if you don't give a number? What if you tell it your name instead? What happens if you give several numbers?

// thinkofanumber.cpp (click here to download)

// This program illustrates cin
#include <iostream>

using namespace std;

int main() {
  int n;
  cout << "Please think of a number." << endl;
  cout << "What is your number?" << endl;
  cin >> n;
  cout << "The number you thought of was " << n << endl;
  return 0;
}

Here is the same program, but for a double value. See if you can find any limitations.

// thinkofadouble.cpp (click here to download)

// This program illustrates double
#include <iostream>

using namespace std;

int main() {
  double x;
  cout << "Please think of another number." << endl;
  cout << "What is your number?" << endl;
  cin >> x;
  cout << "The number you thought of was " << x << endl;
  return 0;
}