C++ Input

Since most of my examples have focused on output, I need to fill in the gap and explain input.

The user is allowed to enter data from a command line using another feature from that iostream package we include in all of our programs.

Basically, we will use cin to read something from the console (another term for the user keyboard). The input will be processed as a stream of characters. That stream will normally end when the user presses the “Enter” (or “Return” key, but it also ends with any “whitespace” character, including a space and a tab. This can cause confusion, so read along.

The easiest way to input something is to first determine what data type you want to input. Typical things we can read at the moment are integer numbers (optionally with a minus sign) or floating point numbers with a decimal point. Note that those floating point numbers can also be entered using “scientific” notation.

To set up for user input, first you create a container to hold the data item the user will provide:

int ival;

With this container available, we can get user input by doing this:

cin >> ival;

Note

The input operator is the “>>” symbol. The idea is that the user is on the left, and the computer on the right. If we are sending data to the user the marker points toward that user. If we are sending data into the computer, the marker points to the right.

The characters the user types will be collected up to the end of the line if they hit “Enter”, or up to any character that is not a valid character for an integer. The user can back up and make corrections, but once they hit the “Enter” key that string of characters will be processed and converted into a binary integer. The result of that conversion will be stored in the ival variable.

When you want to get user input, it is important that you provide a “prompt” string, telling them what to do. Users will not know what you expect if they cursor just sits there blinking.

The program will stall at the cin line until the user enters something valid.

cout << Enter an integer : ";
cim >> ival;

Notice that I did not end the prompt line with an endl. That is because I want the cursor to sit there blinking at them right after the prompt message (notice the space as well at the end of that line). This is what most people expect to see so we should not surprise them.

Validating Input

Warning

Never trust the user to do what you ask them. At this point, we do not know enough to deal with silly input, but we will explore that later. For now, you can assume that the users of your program will be smart enough to do what you ask.

You should go back and read the Input Validation lecture again. You have the C++ tools needed to convert the pseudo-code examples in that lecture to real C++ code now.

Other Data Types

The type of container you ask cin to load defines what the user is allowed to type in. The converted value they type in must fit in that container, or you will get an error. Again, we will not worry about this as well.

Multiple Inputs

You can enter more than one item on a line. The user can type in more than one thing on a single line, as long as there is whiyr pace between the items they type. Here is an example:

int quantity;
double unit_price;

cout << "Enter an number of items, and the price per item : ";
cin >> quantity >>  unit_price;

The user could type in this line:

12 1.25

Or this would work

12
1.25

The system skips any “whitespace”, including new line markers and hunts down the next item to read.

Playtime

You should set up some simple input tests, and see what happens when you run the code. Try good input, and bad input to see what kinds of error messages you get.

This should be enough to get you going on user input. We will deal with those error situations in a later lecture.