-
November 16th, 2009, 08:14 PM
#1
Need help on an array :(
So, I have been given a file that I need to put into an array:
Big Stone Gap,VA
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
I have been told that I need to put the data into an array, and once I do that I can complete all the other tasks. The array is just causing problems for me, as I am not very experienced with C++.. Obviously I need to use getline to get the city name and then take out the other variables. Any help will be much appreciated . Thanks
-
November 17th, 2009, 04:58 AM
#2
Re: Need help on an array :(
These look all the same:
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.
-
November 17th, 2009, 11:56 AM
#3
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();
-
November 17th, 2009, 02:29 PM
#4
Re: Need help on an array :(
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?"
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|