How to use cin for an array in C++ [SOLVED]


How To

Reviewer: Deepak Prasad

In c++ cin array is not performed in one cin statement. In this article we will learn about array input in c++. To understand array input, we first need to understand what an array is and how does it work.

 

Understanding Arrays

Arrays are used to store multiple values, rather than storing similar data in separate variables, which is tedious as well as memory consuming, we create an array, which is a container that contains multiple values. Written below is a code that is the record of 5 students marks in separate variables and then in an array.

#include <iostream>

using namespace std;

int main()

{
    int student1 = 1;
    int student2 = 2;
    int student3 = 3;
    int student4 = 4;
    int student5 = 5;
    cout << "marks of student1: " << student1 << endl;
    cout << "marks of student2: " << student2 << endl;
    cout << "marks of student3: " << student3 << endl;
    cout << "marks of student4: " << student4 << endl;
    cout << "marks of student5: " << student5 << endl;
}

Output of this code is :

marks of student1: 1
marks of student2: 2
marks of student3: 3
marks of student4: 4
marks of student5: 5

 

Now, the code written below is the same but data is stored in an array

#include <iostream>

using namespace std;

int main()

{
    int student[5] = {1, 2, 3, 4, 5};
    cout << "the marks of student 1 are : " << student[0] << endl;
    cout << "the marks of student 2 are : " << student[1] << endl;
    cout << "the marks of student 3 are : " << student[2] << endl;
    cout << "the marks of student 4 are : " << student[3] << endl;
    cout << "the marks of student 5 are : " << student[4] << endl;
}

The output of the following code is :

the marks of student 1 are : 1
the marks of student 2 are : 2
the marks of student 3 are : 3
the marks of student 4 are : 4
the marks of student 5 are : 5

 

Cin Array

If you want input from the user you need to cin array, but cin cannot be directly used in this case, it will show an error like the one given below

#include <iostream>

using namespace std;

int main()

{
    int student[5] = {1, 2, 3, 4, 5};
    cin >> student;
}

Output:

cinarray.cpp:6:12: error: invalid conversion from ‘int*’ to ‘long int’ [-fpermissive]
    6 |     cin >> student;
      |            ^~~~~~~
      |            |
      |            int*
cinarray.cpp:6:12: error: cannot bind rvalue ‘(long int)((int*)(& student))’ to ‘long int&’

So we use a for loop for the input of the array, the loop will run till the size of the array, and take input of array at every index, this is how cin array is executed in C++.

 

The code for cin array is written below :

#include <iostream>

using namespace std;

int main()
{
    int student[5] = {1, 2, 3, 4, 5};

    //cin array
    for (int i = 0; i < 5; i++)
    {
        cout << "enter student marks " << endl;
        cin >> student[i];
    }

    //output 
    cout << "the marks of student 1 are : " << student[0] << endl;
    cout << "the marks of student 2 are : " << student[1] << endl;
    cout << "the marks of student 3 are : " << student[2] << endl;
    cout << "the marks of student 4 are : " << student[3] << endl;
    cout << "the marks of student 5 are : " << student[4] << endl;
}

The output of code of cin array is :

enter student marks 
1
enter student marks 
2
enter student marks 
3
enter student marks 
4
enter student marks 
5
the marks of student 1 are : 1
the marks of student 2 are : 2
the marks of student 3 are : 3
the marks of student 4 are : 4
the marks of student 5 are : 5

 

Conclusion

In this article we studied what are arrays, how do we store elements in arrays and how cin arrays take place. It is executed in a loop unlike other variables.

 

Azka Iftikhar

Azka Iftikhar

She is proficient in multiple programming languages, including C++, GO, and Java, she brings a versatile skillset to tackle a wide array of challenges. Experienced Computer Scientist and Web Developer, she excels as a MERN Stack Expert. You can check her professional profile on GitHub which captures her experience and portfolio.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment