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
You can use String to take input and then convert/use it using Integer. parseInt() . Using this you don't have to allocate an oversized array.
int reqArraySize; printf("Enter the array size: "); scanf("%d", &reqArraySize); After this you may proceed with this interger input array size : for(i=0;i<reqArraySize;i++) scanf("%d",&arr[i]); Hope this helps you.
Input and Output Array Elements Here's how you can take input from the user and store it in an array element. // take input and store it in the 3rd element scanf("%d", &mark[2]); // take input and store it in the ith element scanf("%d", &mark[i-1]);
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:
MAX_SIZE,Hence: use an std::vector!!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With