2

I'm trying to make a class for adding huge integers. I have 2 arrays both of size 40 I wanted to know if there is any way I can take input with out doing the old school method:

for(int i=0;i<40;i++)
{
   std::cin >> arr[i];
}

In this way I have to take input 40 times. Is there a way to take input like we do in cin.getline?

1
  • 1. how do you want to handle the situation in which the input is shorter than 40? 2. how do you represent data in memory? array of integers? array of chars? 3. did you take 40 for the sake of an example or that's your largest supported size? Commented Mar 16, 2016 at 18:25

1 Answer 1

1

No, you can't get an array of integers directly, since there's no overload for this type. You'll have to either use third-party library for parsing or define an additional overload for operator>> and array. Example:

#include <iostream>
#include <array>

template <int  N>
std::istream & operator>>(std::istream & is, std::array<int, N> a)
{
  for(int i = 0; i < N; i++)
    is >> a[i];
  return is;
}

int main()
{
  std::array<int, 10> ar;
  std::cin >> ar;
  for(auto & e : ar)
    std::cout << e << ' ';
  return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.