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

jwpfox
  • 4,786
  • 11
  • 41
  • 42
Nson
  • 119
  • 2
  • 10

5 Answers5

8

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!!

paul-g
  • 3,510
  • 1
  • 17
  • 32
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.

Bathsheba
  • 220,365
  • 33
  • 331
  • 451
  • 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? – paul-g Nov 05 '16 at 11:15
  • Yes you might be correct. Your answer is rather posh, plus one. – Bathsheba Nov 05 '16 at 11:21
1

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().

Sergey Kalinichenko
  • 675,664
  • 71
  • 998
  • 1,399
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>());
Community
  • 1
  • 1
Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
  • 1
    Why show the wrong way to do it? Also, why use the longer istream+back_inserter when you can simply use `std::vector nums(std::istream_iterator(std::cin), {});`? – paul-g Nov 05 '16 at 11:12
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;
}
  • 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`? – zkoza Mar 01 '21 at 23:14