Reverse, max & min, sort, average of an array

Hello Everyone!

So same project, bigger issues...I'm in crunch time and I don't even think I'll be able to turn this code in for credit at this point but I'm determined! Maybe the professor will be merciful.....
Anyways so for this assignment we were to use a text file to fill an array, to feed into a structure. Then we have all these options to mess with it.

I cannot figure out how I would display it in reverse order (from the bottom of the text file up), find the average age, display the oldest animal, or display the youngest animal. Hopefully there is a patient someone out there who could help me. I really need it!

Here's the .txt sample
1
2
3
4
5
6
Goofy dog 8
Minnie mouse 22
Daisy duck 18
Mickey mouse 25
Pluto dog 10
Donald duck 21


and here is the code I have so far:
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
 #include <iostream>  
#include <iomanip>  
#include <fstream>  

using namespace std;  

# define fLength 32  
# define nLength 20  
# define sLength 10  
# define MAX 15  
# define WIDTH 14
# define MAX_NUM_ANIMALS 15  
# define INPUT 3
  
 struct Animals  
 {  
	char	name	[nLength];  
    char	species	[sLength];  
	int		age;  
 };  

ifstream inFile;  
char name[nLength];  
char species[sLength];  
int age;  
char choice[2];

void optionTwo()
{
	Animals arrayOfCritters[MAX_NUM_ANIMALS];
	Animals critter[MAX_NUM_ANIMALS];
	cout << "Name		Species		Age\n";
	cout << "----		-------		---\n";

	for (int critterCount = MAX_NUM_ANIMALS - 1; critterCount > -1; critterCount--)
	{
	inFile >> critter[critterCount].name >> critter[critterCount].species >> critter[critterCount].age;
	}
	while(inFile)
	{
		for (int critterCount = MAX_NUM_ANIMALS - 1; critterCount > -1; critterCount--)
		{
			cout << critter[critterCount].name << setw(WIDTH) << critter[critterCount].species << setw(WIDTH) << critter[critterCount].age;
			cout.unsetf( ios::left );
			inFile >> critter[critterCount].name >> critter[critterCount].species >> critter[critterCount].age;
		}
	}	
}

void optionOne()
{ 
	Animals arrayOfCritters[MAX_NUM_ANIMALS];
	Animals critter;
	inFile >> critter.name >> critter.species >> critter.age; 
		cout << "Name		Species		Age\n";
		cout << "----		-------		---\n";
		
		while(inFile)
		{
		cout << critter.name << setw(WIDTH) << critter.species << setw(WIDTH) << critter.age << endl;
		cout.unsetf( ios::left );
		inFile >> critter.name >> critter.species >> critter.age;
		cout.unsetf( ios::left );
		}
}

 void openFile(ifstream &inFile)  
 {  
    char fileName[fLength];  
	cout << "Please enter the filename: ";  
    cin >> fileName;  
    cout << "\n\n";  
    inFile.open(fileName, std::ios::in);  

    if (!inFile)  
     {  
         cout << "The file '" << fileName << "' could not be opened.\n\n";  
    }  
 }  
 void displayMenu()  
 {  
		cout << "\nMenu:\n";  
		cout << "-------\n";  
		cout << "1 - Display all animals in forward order\n";  
	    cout << "2 - Display all animals in reverse order\n";  
	    cout << "3 - Display all animals sorted by age (youngest first)\n";  
		cout << "4 - Determine average animal age\n";  
		cout << "5 - Determine which animal is oldest\n";  
		cout << "6 - Determine which animal is youngest\n";  
		cout << "Q - Quit the program\n";  
 }  
 int main()  
 {  
    openFile(inFile);  
	while (inFile)  
    {  
	do
	{	
		char choice [2];  
		displayMenu();    
		cout << "\nPlease enter your choice: ";  
		cin >> choice;  

    cout << "\n\n";  
     if (choice[0] == '1')  
     {   
		 optionOne();
	
     }  
     else if (choice[0] == '2')  
     { 
		 optionTwo();
     }  
     else if (choice[0] == '3')  
     { 

     }  
     else if (choice[0] == '4')  
     { 

     }  
     else if (choice[0] == '5')  
     {  

     }  
     else if (choice[0] == '6')  
     {  
   
     }  
     else if (choice[0] == 'q' || choice[0] == 'Q')  
    {  
         cout << "Goodbye!\n\n";  
         system("pause");  
        return 1;  
     }  
     else if (choice[0] < '1' || choice[0] > '6')  
    {  
        cout << "Sorry, but that's not a valid option.\n";  
        cout << "Please choose again\n";  
     }  
     else if (choice[0] != 'q' || choice[0] != 'Q')  
     {  

        cout << "Sorry, but that's not a valid option.\n";  
        cout << "Please choose again\n";  
     }  
    }  
	while(choice[0] != 'q' || choice[0] != 'Q');
	}
	system ("pause");  
	return 0;  
 } 
Well, not a word of thanks (or disapproval, or any response at all) for my previous contribution. It's very difficult to help someone without having a two-way dialogue.

You made various changes to the code I suggested. That's fine, you are free to do so. However, I'm not sure what functionality has been gained in so doing. If you could explain why you are approaching the while loop at line 95 while (inFile) in this particular way, rather than my suggestion of while (choice != 'Q' && choice != 'q') then things might be come clearer on all sides.

To return to your question,
I cannot figure out how I would display it in reverse order (from the bottom of the text file up), find the average age, display the oldest animal, or display the youngest animal.
I would suggest that all of these operations are fairly straightforward if your starting point, rather than a file, is an array.

For example, to display in reverse order, simply step through the array from the last row to the first. To find the average age, step through the array, add up the total of all the ages and divide by the number of rows. To find the oldest animal, step through the array, find the row with the greatest age, and display the details for that row, and so on.

Of course each of those tasks may be broken down into more detail as required.
Topic archived. No new replies allowed.