Reputation: 125
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
Upvotes: 9
Views: 24256
Reputation: 1
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;
}
Upvotes: 0
Reputation: 3877
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, " "});
}
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
!!
Upvotes: 10
Reputation: 409136
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);
}
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>());
Upvotes: 1
Reputation: 726479
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, ',');
}
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()
.
Upvotes: 2
Reputation: 234635
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.
Upvotes: 5