using struct items

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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.
I take it that since animalCount is an int, you can't just add it to a string... stringstreams can be used for that:

1
2
3
4
5
6
7
8
9
#include <sstream>
//...
int n = 100;
ostringstream out;
out << n;
string number = out.str();
//...
string result;
result += number;
Last edited on
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.
Topic archived. No new replies allowed.