3

What is the easiest way to read space separated input into an array?

//input:5
        1 2 3 4 7



int main() {
    int n;
    cin>>n;
    int array[n];
    for (int i =0;i<n;i++){
        cin>>array[i];
    }
    cout<<array; 
    return 0;
}  

I tried the code above but the output is 0x7ffe42b757b0.

6
  • 1
    Define doesn't work. It's the most useless description of the problem. Commented Apr 7, 2016 at 9:06
  • "it doesn't work" doesn't tell us anything. What actually happens? Does it crash? Does it read everything into the first element of the array? Something else? Commented Apr 7, 2016 at 9:07
  • 1
    On the other note, I noticed the problem (I think). Variable length arrays are not standard C++. Consider using std::vector. Commented Apr 7, 2016 at 9:07
  • Wild guess: You are confused by the output and put the blame on the wrong piece. cout << array doesn't do what you probably want it to. Commented Apr 7, 2016 at 9:08
  • 1
    Possible duplicate of Printing an array in C++? Commented Apr 7, 2016 at 9:15

3 Answers 3

5

The problem lies with your printing. Since array is a pointer, you are only printing an address value.

Instead, do this:

for (int i =0;i<n;i++){
    cout<< array[i] << " ";
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much.That's the problem.I have had many problems like this since I changed to C++ from Python.
@J.Ren. C++ has many features to offer that prevent you from having to use fixed size arrays. In my example you can put an input string of any size.
3

You can do that by passing std::istream_iterator to std::vector constructor:

std::vector<int> v{
    std::istream_iterator<int>{std::cin}, 
    std::istream_iterator<int>{}
    };

std::vector has a constructor that accepts 2 iterators to the input range.

istream_iterator<int> is an iterator that reads int from a std::istream.

A drawback of this approach is that one cannot set the maximum size of the resulting array. And that it reads the stream till its end.


If you need to read a fixed number of elements, then something like the following would do:

int arr[5]; // Need to read 5 elements.
for(auto& x : arr)
    if(!(std::cin >> x))
        throw std::runtime_error("failed to parse an int");

6 Comments

This is a cool construction, can you explain what it does exactly?
@Chiel Added references for you.
I don't manage to run this. If I put this in my example, it never stops asking for input.
Your example does not work with clang. It never ends the cin
@Chiel Use std::copy_n, see this article.
|
1

array variable always give base address of an array

for (int i =0;i<n;i++){
    cout<< array[i] << " ";
    cout<<*(array+i)<< " ";
}

Both cout statements will print same only. in short array[i] compiler will internally convert into *(array+i) like this only

in the second cout statement from base address of an array it won't add value,since it is an pointer it will add sizeof(int) each time for an increment

1 Comment

@O'Neil you are correct!.i knew it but missed it . thanks for identifying that mistake

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.