Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple array length question 1

Status
Not open for further replies.

yaids

Programmer
Apr 1, 2005
11
AU
hi i want to loop through an array and need to get its length, ie. the number of elements stored inside. however, the compiler doesnt like num.length(); it says:
request for members 'length' in 'num', which is of non-aggregate type 'double[10]'.
i have no idea what this means. if anyone could give me some help or tell me of an alternate way, it would be greatly appreciated.
thanks in advance.


#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char *argv[]){

char buffer[255];
ifstream input(argv[1]);
double num[100];
int i = 0;
double early = 0;
double late = 0;
int length = num.length(); //doesnt work!!!

while(!input.eof()){
input.getline(buffer, 100);
num = atof(buffer);
cout<<num<<endl;
i++;
}
}

 
Error said:
request for members 'length' in 'num', which is of non-aggregate type 'double[10]'
That says num is not a class or struct (or union); it's an array.

That means you can't use the dot operator on it because objects that are not classes or structs (or unions) have no members to access.


You already have the length. You used it to declare the array. You should declare it as an enum or using a [tt]#define[/tt] statement, though.

If you need to pass the array to a function, you'll need to pass the length along with it.


What you probably want to be doing is using an [tt]std::vector<double>[/tt] instead. A vector has a member function to get its length, but it's called [tt]size()[/tt] instead.
 
Myself said:
You should declare it as an enum or using a #define statement, though.
To clarify, the word "it" in that statement is meant to refer to the length.
 
Hey thanks for your reply.

You say I already have the length when I declared the array, but I thought that is just setting the size of it. The thing is, can you find out how many elements are being stored in the array? Is there any way to do this just by using an array?

I'm sorry if my questions seem kind of dumb, cos I'm quite new to programming...
 
Sorry, maybe if I explain what I'm trying to do it might help a bit.

I basically want to read a text file with times in them.
So:

19.30
20.00
16.00
21.30
19.00

I want to read in each time, find out the earliest and latest times and then increment in half hour blocks from the earliest to the latest.
So in this case the earliest would be 16.00 and from there, I would increment (16.30, 17.00, 17.30....etc) until I reach 21.30.

Thanks in advance.
 
Well, you'll have to keep a variable for the number of elements as well as the size of the array.


Again, you probably want to be using a vector. Why?
[ul]
[li]it keeps track of its own size[/li]
[li]it keeps track of the number of elements it contains[/li]
[li]it dynamically resizes when necessary[/li]
[li]it provides functions like [tt]push_back[/tt] that keep you from needing to care about the length[/li]
[li]it provides an iterator interface that keeps you from needing to know its length to loop over the elements[/li]
[li]it can be used with STL algorithms that, among other things, can compute the maximum and minimum values in the vector automatically[/li]
[/ul]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top