Hi to all
I have a quick question. I am going to fill an array with some inpuit of type char using member function cin.getline. After I fill an array ,I would like to do "for" loop so I need to know what is curently the size of an array. Is it possible. Looks like stupid question because I initialized the array that contains 250 elements (and what I know so far I can't chang ethe size of array, isn't right?). But if I would enter let sey 25 characters. Is the size going to change?Is it possible. If so how may I got the current size of the array. If not what is the best choice to fill an array (maybe vector ?) ,tht will let me know the current size of array
Thank you

there is a function in c++ called strlen() which tells you the length of an array... if not, you can use a while loop with a counter that exits when array[i]=='\0'

Hi to all
I have a quick question. I am going to fill an array with some inpuit of type char using member function cin.getline. After I fill an array ,I would like to do "for" loop so I need to know what is curently the size of an array. Is it possible. Looks like stupid question because I initialized the array that contains 250 elements (and what I know so far I can't chang ethe size of array, isn't right?). But if I would enter let sey 25 characters. Is the size going to change?Is it possible. If so how may I got the current size of the array. If not what is the best choice to fill an array (maybe vector ?) ,tht will let me know the current size of array
Thank you

The best choice would be a std::string - which is a container specifically designed for holding characters (Far more appropriate to the task than a vector). You can use a std::string with getline() too - eg,

#include <iostream>
#include <string>

int main()
{
    std::string str;
    std::getline(std::cin, str);
    std::cout << str << " length: " << str.length();
}

>Is it possible.
When it comes to C++, if you ask this question the answer will nearly aways be yes. You can do damn near anything with C++.

>If so how may I got the current size of the array.
You're reading a C-style string, and C-style strings always end with a '\0' character. So if you loop until you find one, you know the size of the array:

int i;

for ( i = 0; i < 250; i++ ) {
  if ( array[i] == '\0' )
    break;
}

cout<<"The array is "<< i <<" characters long\n";

The strlen function from <cstring> does this for you.

> there is a function in c++ called strlen() which tells you the length of an array
That would only be for char arrays, not any array.

Got it ).Thanks a lot guys. You are very helpfull. Appreciate it

note : i searched google with "c++ length of array" and hit this thread, so dont tell me why i post to this thread although it is old.

to find the length of an array :

#include <iostream>
using namespace std;

int myArray[] = {5,6,7,8,9};

int lengthOfArray = sizeof(myArray) / sizeof(int);

char myArray2[] = {'a','b','c','d'};

int lengthOfArray2 = sizeof(myArray2) / sizeof(char);

int main()
{
  cout << lengthOfArray << endl;
  cout << lengthOfArray2 << endl;
  return 0;
}

output is :
5
4

To conclude:

The generic solution to array length is :

int lengthOfArray = sizeof(myArray) / sizeof(typeofArrayItem);

>int lengthOfArray = sizeof(myArray) / sizeof(typeofArrayItem);
While correct in and of itself, this doesn't answer the original question at all. It only works for actual arrays (as opposed to simulated arrays using pointers), so it's somewhat restricted in that you can't use it on "array" function parameters. Further, it always returns the full capacity of the array, not the current number of valid items, which was what the OP wanted to know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.