In our previous tutorial, we learned about the C++ String datatype. Until know, our C++ programs haven’t been that interactive. In this tutorial we are going to fix that. We are going to talk a little more in depth about gathering input from the user and displaying the output to the terminal.
C++ Standard Output
If you recall, we have been using the cout object to display information on to the screen. Below, I’ll describe various ways to output strings, the contents of variables, expressions, and more.
Basic output with endl
Below is a simple example of printing out the string “Hello, World!” to the terminal accompanied by a newline.
1 2 3 4 5 | // We first print out Hello World cout << "Hello, World!"; // We then add the following to create a new line cout << endl; |
Basic output with variables
Now, we are going to display the contents of a variable. Notice how we can output more than one item with a single cout object.
1 2 3 | int num = 5; cout << num << endl; |
Basic output with math expressions
We can also output arithmetic expressions. This by the way, is something you will hardly ever use but its good to know anyways since you’ll always want to save the result of an expression in a variable.
1 | cout << 5 * 4 + 3 << endl; |
Basic output with boolean expressions
Finally, we will demonstrate a simple example with a boolean expression. This will not print out true or false. It will print out either a 1 for true, or 0 for false. You also need the parenthesis, else the < operator will give you an error.
1 | cout << (1 < 3) << endl; |
C++ Standard Input
For standard output we used the object cout and for standard input, we will use the object cin. Before we start with examples, there is are a couple of things you must know.
- User input is usually done through the keyboard and the terminal. Your program will halt at each user input and keep executing when the user enters a value and then presses enter
- The cin object will read in the type of object you tell it to. Therefore, if you wish to have a char, it will read a char. Same with an integer, float, and a string. If you mix things up, you will receive undesired behavior
- The user might provide you with invalid values and therefore require you to do error checking. We will leave this for a later topic.
Reading in a value
Below is an example of reading in a single and multiple values. Try it by changing the object type.
1 2 3 4 5 6 7 8 9 10 11 | // We declare and initialize the variable num to -1 int num = -1; // We prompt the user for input cin >> num; // We declare three strings string a, b, c; // We read three strings from input cin >> a >> b >> c; |
Going further with input and output
Now that you understand the basics of input and output why not do the following?
- Prompt the user for his First Name, Last Name, Age, City, and State in the following fashion.
Enter your first name: Enter your last name: Enter your age: Enter your city: Enter your state:
Then, output the results in the following fashion.Hello firstname lastname! I see you are age years old and live in city, state.
- Now try declaring a variable without initializing it. Then output it. What happened?
- Try reading in an int value, but give it a string. Output the variable and what happens?

