[Solved] How to Use cin for an Array in C++

Introduction

In this tutorial, we will learn about how to use cin for an array in C++. In C++ programming language, both cin and cout provides a convenient way to interact with the user and display program output. When we take array input in C++, we use cin array instead of single cin statement. We will understand the limitation of single cin statement and when to use cin array. So let us get started with this tutorial.

[Solved] How to Use cin for an Array in C++

 

What is cin and cout in C++?

In C++ programming language, cin and cout are predefined objects that are used for input and output operations, respectively.- They are part of the C++ standard Library and are typically associated with the input and output streams.  Let us understand both the term in more depth.

CIN

cin stands for “console input”. It is an input stream object that is used to read data from the standard input, typically the keyboard. You can use the cin in combination with the extraction operator >>” to read different types of values (integer, strings, floats etc) from the user or other input sources.

Below is the example to read an integer value from the user

int userVal;
cin >> userVal;

 

COUT

cout stands for “console output”. It is an output stream object that is used to send data to the standard output which is typically the console or terminal window. You can use cout in combination with <<“ to display data on the screen.

Below is the example to print a message on the console.

cout << "Hello There !" <<endl;

 

[Solved] How to Use cin for an Array in C++

Also read: Installing node.js on Linux [Step by Step Guide]

We will understand the cin single statement and cin array with the help of examples. Let us write a code which will store the id of 5 Employees in 5 different variables as shown below. It will then  print the Employee’s ids on the console.

Program-1

#include <iostream>
using namespace std;

int main() {
int employee1 = 220;
int employee2 = 221;
int employee3 = 222;
int employee4 = 223;
int employee5 = 224;

cout << "Employee1 ID: " <<employee1 <<endl;
cout << "Employee2 ID: " <<employee2 <<endl;
cout << "Employee3 ID: " <<employee3 <<endl;
cout << "Employee4 ID: " <<employee4 <<endl;
cout << "Employee5 ID: " <<employee5 <<endl;
}

 

OUTPUT

Employee1 ID: 220
Employee2 ID: 221
Employee3 ID: 222
Employee4 ID: 223
Employee5 ID: 224

As you see the output above, it prints the ids of all five employees. Although this method does the job, but it is never recommended to store the same type of data in different variables as it consumes memory and also becomes complex to manage. Therefore, we use array to store same type of data. Let us rewrite the above code but this time we will store the Employee’s ids in an array.

 

Program-2

#include <iostream>
using namespace std;

int main() {

int empArray[5] = {220, 221, 222, 223, 224};

cout << "ID of Employee1: " << empArray[0] <<endl;
cout << "ID of Employee2: " << empArray[1] <<endl;
cout << "ID of Employee3: " << empArray[2] <<endl;
cout << "ID of Employee4: " << empArray[3] <<endl;
cout << "ID of Employee5: " << empArray[4] <<endl;
}

OUTPUT

ID of Employee1: 220
ID of Employee2: 221
ID of Employee3: 222
ID of Employee4: 223
ID of Employee5: 224

As you see, the code returns the similar output. What if we want user to provide the Employee’s ids instead of defining it in the code?. In such case, we use cin array which allows us to read the multiple same type of data from user and store in in an array. We then use this array for further processing. We will again write the above code and this time we will ask user to provide Employee’s ids as CLI input to this code.

 

Program-3

#include <iostream>
using namespace std;

int main() {

int empArray[5] = {};

for (int i = 0; i < 5; i++)
{
cout << "Enter Employee" << i + 1 << " ID" << endl;
cin >> empArray[i];
}

cout << "Employee1 ID: " << empArray[0] <<endl;
cout << "Employee2 ID: " << empArray[1] <<endl;
cout << "Employee3 ID: " << empArray[2] <<endl;
cout << "Employee4 ID: " << empArray[3] <<endl;
cout << "Employee5 ID: " << empArray[4] <<endl;
}

 

OUTPUT

Enter Employee1 ID
220
Enter Employee2 ID
221
Enter Employee3 ID
222
Enter Employee4 ID
223
Enter Employee5 ID
224

Employee1 ID: 220
Employee2 ID: 221
Employee3 ID: 222
Employee4 ID: 223
Employee5 ID: 224

As you see the output above, when you execute the code, it will ask user to enter the id of each employee. It will store all the user input in an array and print the Employee’s ids on the console.

 

Summary

We have successfully used cin to read the user input and store in an array. If you do not have the C++ configured in you machine, you can follow the guide How to Install GCC(C and C++ Compiler) on Ubuntu 20.04 LTS to install and configure C++ in your machine.

 

 

Leave a Comment