8.11. Getting user input

The programs we have written so far are pretty predictable; they do the same thing every time they run. Most of the time, though, we want programs that take input from the user and respond accordingly.

There are many ways to get input, including keyboard input, mouse movements and button clicks, as well as more exotic mechanisms like voice control and retinal scanning. In this text we will consider only keyboard input.

In the header file iostream, C++ defines an object named cin that handles input in much the same way that cout handles output. To get an integer value from the user:

int x;
cin >> x;

The >> operator causes the program to stop executing and wait for the user to type something. If the user types a valid integer, the program converts it into an integer value and stores it in x.

If the user types something other than an integer, C++ doesn’t report an error, or anything sensible like that. Instead, it puts some meaningless value in x and continues.

Fortunately, there is a way to check and see if an input statement succeeds. We can invoke the good function on cin to check what is called the stream state. good returns a bool: if true, then the last input statement succeeded. If not, we know that some previous operation failed, and also that the next operation will fail.

Thus, getting input from the user might look like this:

The active code below is an example of what getting input from the user might look like. Feel free to change 42 to other values!

cin can also be used to input a string:

string name;

cout << "What is your name? ";
cin >> name;
cout << name << endl;

Unfortunately, this statement only takes the first word of input, and leaves the rest for the next input statement. So, if you run this program and type your full name, it will only output your first name.

Because of these problems (inability to handle errors and funny behavior), I avoid using the >> operator altogether, unless I am reading data from a source that is known to be error-free.

Instead, I use a function in the header string called getline.

string name;

cout << "What is your name? ";
getline (cin, name);
cout << name << endl;

The first argument to getline is cin, which is where the input is coming from. The second argument is the name of the string where you want the result to be stored.

getline reads the entire line until the user hits Return or Enter. This is useful for inputting strings that contain spaces.

The active code below is an example of what getting input from the user might look like using getline. Feel free to change “Harry Potter” to other values!

In fact, getline is generally useful for getting input of any kind. For example, if you wanted the user to type an integer, you could input a string and then check to see if it is a valid integer. If so, you can convert it to an integer value. If not, you can print an error message and ask the user to try again.

To convert a string to an integer you can use the atoi function defined in the header file cstdlib. We will get to that in Section 15.5.

You have attempted of activities on this page