string load_data(string filename)
{
ifstream InFile; //used for bringing stuff in from the file
animalCount = 0;
string results;
InFile.open(filename.c_str()); //opening the file
if(!InFile) //if the file doesn't exist
return"No such file exists.";
while(InFile)
{
InFile >> animal_Array[animalCount].id;
InFile >> animal_Array[animalCount].name;
InFile >> animal_Array[animalCount].species;
InFile >> animal_Array[animalCount].age;
InFile >> animal_Array[animalCount].weight;
animalCount++;
}
results = "File opened successfully.\n";
results += "There are animals to view.";
return results;
}
/*
view_animals
When the user clicks on the "View" button, this function is called
with the desired animal type passed into the function (aka the string
called animal_type). This function will iterate through the array of
animals to find those with with a matching type.
Precondition:
The animal array has been loaded from file.
The user has entered the desired animal_type.
Postcondition:
If the array of animals is empty, an error message is returned.
If matches were found, a string containing those animals is returned.
If matches were not found, an error message is returned.
*/
string view_animals(string animal_type)
{
string result = "";
// 1) Iterate through animal array and add matches to result
for(int i = 0; i < animalCount; i++)
{
if(animal_type == animal_Array[i].species)
{
result += " ID Number: " + animal_Array[i].id + "\n";
result += " Name: " + animal_Array[i].name + "\n";
result += " Species: " + animal_Array[i].species + "\n";
result += " Age: " + animal_Array[i].age + "\n";
result += " Weight: " + animal_Array[i].weight +"\n";
result += "\n";
}
}
// 2) If none found, set result to an error message
if(result == "")
result = "Not a match.";
// 3) return result string
return result;
}
/*
average_weight
When the user clicks on the "View" button, this function is called
with the desired animal type passed into the function (aka the string
called animal_type). This function will return the average weight of
those animals of the same type in the array.
The task at hand might require converting string data into numeric data.
The following function might be useful:
http://www.cplusplus.com/reference/clibrary/cstdlib/atof.html
Take careful note of what arguments this function takes.
Precondition:
The animal array has been loaded from file.
The user has entered the desired animal_type.
Postcondition:
If the array of animals is empty or no matches were found,
an error message ("N/A") is returned.
If matches were found, a STRING containing the average is returned.
Like the other functions provided, you cannot change the return type
or argument list.
*/
string average_weight(string animal_type)
{
string result = "N/A";
return result;
}
/*
oldest_animal
When the user clicks on the "View" button, this function is called
with the desired animal type passed into the function (aka the string
called animal_type). This function will iterate through the array of
animals to find the oldest animal (his or her name) of that type.
Precondition:
The animal array has been loaded from file.
The user has entered the desired animal_type.
For simplicity's sake, if there is a tie, returning just one of the
names is satisfactory.
Postcondition:
If the array of animals is empty or no matches were found,
an error message ("N/A") is returned.
If matches were found, a string containing the name of the oldest is returned.
*/
string oldest_animal(string animal_type)
{
string oldest = "N/A";
return oldest;
}
I have several questions. The first deals with the view_animals function. How do I get the "animalCount" to print off along with the results? And how do I get it to print the animal that occurs most often?
The next is with the other two functions. I don't know how to use the information already gathered to find what I need. How do I get it? Thanks everyone, and I have the rest of the code if any one needs more.
So, it appears that your animal structure stores the age and weight as strings, too. That's lame; it would be so much easier to do these next 2 parts if they were just numbers.
Basically, use what you have working from your view_animals function: you traverse the array and when you match the right animal type you take action.
The actions:
For the average, you need to tally up the weights and count how many there are. Then find and return the average.
For the oldest, you need to store the greatest age (start at 0). For each animal, if its age is greater than the stored greatest age then update then store a new greatest age. Then return the greatest age.
Now, the problem that you will encounter is that you need to convert age and weight from a string to a number before doing any of the actions. I personally would use an istringstream (similar to the ostringstream above). However, it looks like you need to use the atoi function. Pass it a C-style string (char *) and it returns a double. The string class has a member that returns a C-style string; check the reference section for the details.