9

Input : 5

long long n;
cin>>n;
long long a[n];
for(long long i=0;i<n;i++){
    cin>>a[i];
}

How do I input elements of the array without inputting n?

This is what I look for :

Input : 1,2,3,4,5

cout << a[0] 

output:

1

4
  • See en.cppreference.com/w/cpp/container/vector (specifically, the example at the bottom of the page) Commented Nov 5, 2016 at 10:52
  • do u have a thought on upper limit of the array then maybe I can help you. Commented Nov 5, 2016 at 10:53
  • No, no. You don't understand. I want to cin the elements without cin the n itself. Commented Nov 5, 2016 at 10:54
  • @try using the cin as a conditional statement in the while loop. Commented Nov 5, 2016 at 10:59

5 Answers 5

10

The standard input filter loop in C++ is while(cin >> a) - this will read until there is no more input, or other bad things happen:

#include <vector>
#include <iterator>
#include <iostream>
int main() {
  std::vector<int> nums;
  while (std::cin >> a) {
    nums.push_back(a);
  }
  std::copy(nums.begin(), nums.end(), ostream_iterator<int>{cout, " "});
}

You could also use a one liner with input iterators - the shortest way to read elements in a vector:

#include <vector>
#include <iterator>
#include <algorithm>
#include <iostream>

int main() {
  std::vector<int> nums(std::istream_iterator<int>(std::cin), {});
  std::copy(nums.begin(), nums.end(), std::ostream_iterator<int>{std::cout, " "});
}

See Ideone example here

Assuming however that you wish to ignore all this C++ awesomeness, strongly discouraged IMHO, you can just:

#include <iostream>
int main() {
  const int MAX_SIZE = 100;
  int nums[MAX_SIZE]; 
  int a;
  int i=0;
  while (std::cin >> a) {
    nums[i++] = a;
  }

  // do your output
}

Note that you will:

  1. need to guess the MAX_SIZE,
  2. or manually handle reallocation once you read more elements than MAX_SIZE;

Hence: use an std::vector!!

Sign up to request clarification or add additional context in comments.

Comments

5

Use a std::vector<long long> a; instead.

Then use long long temp;, cin >> temp;, and a.push_back(temp);.

This means that the vector will automatically grow as you add more data. There are in a sense smarter ways, but my way carries the advantage of clarity.

These days the cool cats don't worry about repeated push_back as they trust a C++ standard library implementation to keep the memory nice and unfragmented for you. But if you must, then std::vector does allow you to set an initial capacity via its reserve method: you can still grow your vector beyond that if you wish.

2 Comments

I think the question is more about how do you read the elements without inputting the number beforehand, rather than how do you store them?
Yes you might be correct. Your answer is rather posh, plus one.
2

You need to know where the input ends. In your case, it looks like the input fits on a single line. You can read that line, construct a string stream from it, and read comma-separated items from it as follows:

string line;
getline(cin, line);
istringstream iss(line);
vector<long long> a;
long long tmp;
while (iss >> tmp) {
    a.push_back(tmp);
    iss.ignore(1, ',');
}

Demo.

Note how the above uses a std::vector<long long> instead of an array. This approach lets you manage storage for your data with a simple call of push_back, and know how much data you have entered by examining size().

Comments

1

There are a few ways. The most important is that if you don't know the number of values you need to use a container that can grow as needed. For that you have std::vector (as mention by others already).

The naive way to use a vector and read input would be something like

std::vector<int> vector;
while (!std::cin.eof())
{
    int value;
    std::cin >> value;
    vector.push_back(value);
}

But the above loop is wrong!

Using a similar approach to the above loop (but working) would be something like

std::vector<int> vector;
int value;
while (std::cin >> value)
{
    vector.push_back(value);
}

However C++ have many nice utility functions and classes that can make it even simpler.

Using the standard algorithm function std::copy and a few iterator helpers (std::istream_iterator and std::back_inserter) we can write

std::vector<int> vector;
std::copy(std::istream_iterator<int>(std::cin),
          std::istream_iterator<int>(),
          std::back_inserter(vector));

It can, as noted by paul-g, be even simpler since there is a vector constructor overload that takes an iterator range, so all that's really needed is

std::vector<int> vector(std::istream_iterator<int>(std::cin),
                        std::istream_iterator<int>());

1 Comment

Why show the wrong way to do it? Also, why use the longer istream+back_inserter when you can simply use std::vector<int> nums(std::istream_iterator<int>(std::cin), {});?
0

Try using of an array of pointers(More generic way to allocate array elements dynamically) :

#include <iostream>

using namespace std;

int main()
{
    int *Arr[1000],i=0,sizeofArr=0;
    while(1){
        Arr[i] = new int;
        cin >> *Arr[i];
        if(cin.get() == '\n'){       //exit the loop if ENTER is pressed
            break;
        }
        i++;
        sizeofArr++;
    }

    for (int j=0;j<=sizeofArr;j++){
        cout << *Arr[j] <<" ";
    }

    return 0;
}

1 Comment

This is the worst answer of all. Do not use pointers if you don't need them. Also, the loop break condition cin.get() == '\n' is sloppy: what if a number is not followed directly by \n?

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.