Elementary Problems in C++

Here is a simple question (not simple for a beginner):-
How to take n space separated integers as input in c++ without any clumsy code, in a simple and efficient manner?

I previously used python for programming and it has a neat way of doing that but I couldn’t find anything like that in c++. On searching through the net I found some code but surprisingly it was larger than the actual code for a problem (neither easy to implement nor to understand).

I would be grateful if you could tell the code that you use and explain the reason behind it.

If n is not large,

cin>>a>>b>>c>>d>>e>>f>>g;

If n is large,

store it in an array, or use values online

int val;
int arr[n];
for(int i=0;i<n;i++)
{
cin>>val;
arr[i]=val;
}

1 Like

Assuming we already know the value of N:

vector<int> values(N);
for (auto& value : values)
     cin >> value;
3 Likes

Just a query… If I do keep entering integers even after having already entered N integers the program just keeps taking it, so does it result in any error on submitting it as a solution?

How are you so sure that your program just keeps taking the extra integers?

If n is given then idk what’s the problem,
Use stringstream if n is not given.