Re: Need help on an array :(
These look all the same:
Quote:
Red-tailed_Hawk 1
Mourning_Dove 66
Red-bellied_Woodpecker 5
Yellow-bellied_Sapsucker 1
Downy_Woodpecker 4
Pileated_Woodpecker 2
Blue_Jay 8
American_Crow 89
Carolina_Chickadee 8
Black-capped_Chickadee 6
Tufted_Titmouse 12
Red-breasted_Nuthatch 2
White-breasted_Nuthatch 9
Carolina_Wren 3
American_Robin 1
Northern_Mockingbird 1
European_Starling 5
Eastern_Towhee 3
Field_Sparrow 13
Fox_Sparrow 1
Song_Sparrow 8
White-throated_Sparrow 11
Dark-eyed_Junco 9
Northern_Cardinal 30
Purple_Finch 7
House_Finch 6
American_Goldfinch 29
but this is different
So, what is the format of this input file? I'm confused.
Re: Need help on an array :(
OK, as you wrote, the first line in the file is some sort of header with the city name & state; the rest of the file consists of birdies and an index or count.
Seems to me that it would make most sense to split each entry and store in two arrays, with any given array index pointing to a bird and its index. Otherwise, if you simply create an array with the elements containing each entire line, any repeated search on the array for either of the data pairs will require code to split up the stored line.
I would use a vector rather than an array - vectors are easier to use and manipulate, and errors are handled better. An array is something like a vector with no brakes.
Here, sample declarations:
std::vector<CString>bird_name;
std::vector<int>bird_no;
If the number field is just a dumb identifier and not an index, you could also store it as a CString.
If you read each line and split on the space(s) into str1 and str2, building up the vectors is straightforward. For each line, once split, the vectors are simply built up thus:
bird_name.push_back(str1);
bird_no.push_back((int)str2); //storing as int
As long both push_backs are done in pairs, the vector elements will be in sync. One should always check for this and other validity when reading the input file.
Once done, you can access the i'th element in a vector thus:
cout<<bird_name[i]<<" "<<bird_no[i]<<endl;
The number of elements is in bird_name.size();
Re: Need help on an array :(
Quote:
Originally Posted by
rsnyder
So, I have been given a file that I need to put into an array...
Your first question should be: "into an array of WHAT?"