Elias S.
Elias S.

Reputation: 231

How do I use cin for an array

When I run this code

#include <iostream>
#include <string>
#include <math.h>
using namespace std;

int main() {

    int Array[100];

    cin >> Array;

    return 0;
}

I get the following error message at the cin line:

Invalid operands to binary expression ('std::__1::istream' (aka 'basic_istream') and 'int *

Why can't I directly input an array? And how do I fix the problem?

Upvotes: 10

Views: 75625

Answers (6)

fhaus
fhaus

Reputation: 146

For more modern C++ approach:

#include <algorithm>

and do

std::for_each(
    std::begin(Array), std::end(Array), 
    [](auto& elem) { cin >> elem; }
);

or you could use it as operator>> overload

#include <iostream>
#include <algorithm>

template<typename T, size_t Size>
std::istream& operator>>(std::istream& in, T (&arr)[Size])
{
    std::for_each(std::begin(arr), std::end(arr), [&in](auto& elem) {
        in >> elem;
    });

    return in;
}

int main() {

    int Array[100] = { 0 };

    std::cin >> Array;

    return 0;
}

Upvotes: 12

Ahmed Yousif
Ahmed Yousif

Reputation: 2348

you should use loop to enter num by num

#include <iostream>

int main() {
    int Array[100];
    std::cout<<"enter Numbers Here:"<<endl;
    for (int i=0; i<100; i++)
       std::cin>>Array[i];
    return 0;
}

Upvotes: 4

Jad Ghadry
Jad Ghadry

Reputation: 265

There is no possible way to cin an array without overloading the >> operator. What you could do however, is declare it in the following fashion

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

int main() {

    const int size = 100;

    int array[size];

    for (int i = 0; i < size; i++) {
        cout << "Enter number #" << i+1 << ": ";
        cin >> array[i];
    }

    return 0;
}

Upvotes: 1

Caleth
Caleth

Reputation: 62704

One can write an overload of >> to read into a c-style array, and then your main will work fine.

template <typename T, std::size_t N>
std::istream & operator>>(std::istream & is, T (&arr)[N])
{
    std::copy_n(std::istream_iterator<T>(is), N, arr);
    return is;
}

Upvotes: 7

Sanjeev
Sanjeev

Reputation: 358

You need to iterate through each array element to get the value so you should use the iterator or loop (for,while or do while). There is no direct way to get the array value as whole.

Let me try to explain it little further, Array is just a continuous memory which can hold number of elements of defined type. There is no way to know the number of elements needs to be store at runtime or in simple term there is no way to know how big an array is, how many elements it can contain. That's the reason array overflow is very common problem as there is no end delimiter for an array and using the array pointer you can go as long as you can. Hope this will help you to understand in better way.

Upvotes: 3

Andrew21111
Andrew21111

Reputation: 908

You should loop the array elements:

for(int i=0; i<100; i++){
  cout<<"Insert element "<<i<<": ";
  cin>>Array[i];
}

However, try to not use uppercase name for variables because they're usually used to name objects/classes and other things

Upvotes: 8

Related Questions