Introducing the Input Stream 'cin'

Learn how we can give input to the program using 'cin'.

As a programmer, whenever we ship software, we never give C++ code files to the end user. We deploy the executable file. If we take a look at the end user version of the executable file below, we should notice something.

Terminal 1
Terminal
Loading...

Yes! There is no way the end user can access the C++ file written by the programmer and change the table’s value to their requirement.

In this lesson, we’ll learn about the cin instruction, enabling our executable file to communicate with the program.

Taking input from the user with cin

In C++, cout is used with the operator <<. The operator << is called an output stream insertion operator where the data values (characters/integers/strings/floating values) are inserted from the program to that stream. The stream acts like a pipe where symbols are inserted from one end, and they flow to the other end. The console extracts the data from the output stream and prints it on the cursor location.

Similarly, cin is used to take input from the console. cin also acts like a stream in which all the input characters are inserted from the console, and the program can extract values using the syntax cin >> SomeVarName.

See the pictorial representation of how the code in the Task.cpp file displays the message and takes the input from the command prompt.

Now let’s use cin in our program below.

We need to enter the values before we click “Run” in the editor below. To enter multiple values, we enter each value followed by a space or an enter key and then the next value.

Press + to interact
#include <iostream>
using namespace std;
//This program takes the input from the user and displays that number
int main()
{
int num1, num2, num3;
cin >> num1; // taking one number from console
cin >> num2 >> num3; // taking two number from console
cout << "Num1 = " << num1 << " Num2 = " << num2 << " Num3 = " << num3 << endl;
return 0;
}

Enter the input below

In the code above, cin in lines 10 and 11 is used to take input from the user and store that input in the variables num1, num2, and num3. The message, along with the values of num1, num2, and num3 is streamed out to the console screen using cout in line 13.

Enter any 3 values above and click “Run” to see the output of the code above.

When taking input for an integer variable, we are bound to write the same type of input. For example: 10, 45, 97, etc. If we input real numbers like 10.5, 36.9, or 65.7 the integer variable only stores 10, 36, or 65 respectively.


Table printing (contd.)

Now that we know what cin is, let’s continue where we left off in the table printing example in the previous lesson. So in the last lesson, we saw that we would have to make even fewer changes in the code if we had a way for the user to input any number that he would like to print the table of.

The program below takes input from the user.

Press + to interact
#include <iostream>
using namespace std;
//This program should print the table according to user input number
int main()
{
int Table;
cout << "The number that we want to display the table of: " << endl;
cin >> Table; // The user, for example, enters 56
cout << Table << " x 1 = " << Table*1 << endl; // prints 56 x 1 = 56
cout << Table << " x 2 = " << Table*2 << endl; // 56 x 2 = 112
cout << Table << " x 3 = " << Table*3 << endl; // 56 x 3 = 168
cout << Table << " x 4 = " << Table*4 << endl; // …
cout << Table << " x 5 = " << Table*5 << endl;
cout << Table << " x 6 = " << Table*6 << endl;
cout << Table << " x 7 = " << Table*7 << endl;
cout << Table << " x 8 = " << Table*8 << endl;
cout << Table << " x 9 = " << Table*9 << endl;
cout << Table << " x 10 = " << Table*10 << endl;
return 0;
}

Enter the input below

In the program above, we take an integer as input from the user in line 11 and store it inside the Table variable. We can print the first ten multiples of any number using the code above. For example, if we enter 56 as input, it will display the table of 56.

So with user input, without having to alter anything in the code, we can display the table of any number we want:

Terminal 1
Terminal
Loading...

Enter any integer number above to view its table.

Exercise

Modify the above program in the code editor given below so that the table is printed up to the 15th multiple.

Some advice: don’t look at the next program immediately! You should be able to do it by yourself. Even if you do understand it, we recommend writing the lines by yourself as the syntax is only memorized by writing it yourself. Only reading through the lesson again and again will not help.

Printing the table till its nth multiple

Let’s write a program that takes input from the user and prints the table of that input (number) up to its 15th multiple.

Press + to interact
/* Don't worry about including the header file and main(), we've already included that.
You just need to add the code for table printing up to the 15th multiple */
{
int Table = 3;
cout << Table << " x 1 = " << Table*1 << endl;
cout << Table << " x 2 = " << Table*2 << endl;
cout << Table << " x 3 = " << Table*3 << endl;
cout << Table << " x 4 = " << Table*4 << endl;
cout << Table << " x 5 = " << Table*5 << endl;
cout << Table << " x 6 = " << Table*6 << endl;
cout << Table << " x 7 = " << Table*7 << endl;
cout << Table << " x 8 = " << Table*8 << endl;
cout << Table << " x 9 = " << Table*9 << endl;
cout << Table << " x 10 = " << Table*10 << endl;
// Add code here
}

Instruction: Add the 5 lines above to print up to the 15th multiple.


In the above code, we printed the table of a number from its 1st to its 15th multiple.

What if we wanted to print the table of a number till its 100th multiple? Or for that matter, 500th multiple?

We would then have to change the code again for each case, which would land us in the same inconvenient position. If we wanted to print the table up to the 100th multiple or the 500th multiple, we’d have to write 100 or 500 lines with the required multiplicand.

This is not nice! How can we handle this?

To avoid this repetition of similar code, we will learn an extremely powerful technique called a loop.