Need to user input an int array whose length is <=10

I need to input an int array whose length is <=10.
For example:
Input -
5 4 1 3 5 65 2

As well as
Input -
1 2

1 2 357 8 76 5 43 2 2 1 .

The loop quits at 10 element, if it comes to 11th element.
I am out of ideas. Any suggestions!

int n;
int arr[n];
for(int i=0;i<n;i++){

Just input n and enter the number of elements you want to input and you are done

That’s the problem… n is variable.

int n;
int v[10];
int cnt = 0 ;
may be this will do the job!!
as far as I understand your problem!!
you can provide link to question also!!

@kk2_agnihotri I am not allowed to share the link due to some regulations. BTW, thanks for the code, but it isn’t working.
The code does not input exactly 10 elements to the array. The code inputs 10 elements or less. The array Whenever the user presses the enter twice, that fixes the length of the array.
I tried this approach:

int a[10], i=0;

This one break the loop if any non -integer is given as input, and the elements given as user input gets saved. But the problem with this one is that this ignores the 10 element restriction on input.

In c++ you can use stringstreams.

#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <vector>
#define ll long long int
#define mp make_pair
#define pb push_back
#define vi vector<int>
using namespace std;
int main() {
    string s;
    int array[10];
    stringstream input(s);
    int x;
    int idx=0;
    while(input>>x && idx<10){
    int n=idx;
    for(int i=0;i<n;i++){
        cout<<array[i]<<' ';

Thanks for the code, but I can only use arrays and cannot use vectors for the problem.

That makes practically no difference, edited to make it an array.

1 Like

It works!!! Thanks a lot!