9

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?

Jad Ghadry
  • 245
  • 2
  • 25
Elias S.
  • 221
  • 1
  • 3
  • 10

6 Answers6

10

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;
}
fhaus
  • 126
  • 8
7

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

Andrew21111
  • 868
  • 8
  • 17
7

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;
}
Caleth
  • 52,200
  • 2
  • 44
  • 75
3

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;
}
Ahmed Yousif
  • 2,298
  • 1
  • 11
  • 17
3

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.

Sanjeev
  • 348
  • 2
  • 9
1

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;
}
Jad Ghadry
  • 245
  • 2
  • 25