I want to calculate the length of an array

#include<iostream>
using namespace std;
int main()
{
int a[1000],n,i=0;
while(cin>>a[i])
{
      i++;
}
for(int j=0;j<i;j++)
{
cout<<a[j];
}

I just want the length.

Did you check if C++ already offers a built-in function for that?

1 Like

The length of your array is 1000, because you declared it with length 1000.
In C++ if you want to get the length of an array, the method is called size: array::size - C++ Reference

3 Likes

Update: I have been informed by my resident C/C++ expert that size might not be the right method. You might need sizeof instead. Google around. There are code examples out there.

2 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.