Well, there are a number of issues here. For one thing, the use of global variables and then passing those variables as parameters is a kind of neither here nor there approach. If you use global variables, there's no need to pass them between functions, as they are already available in the global namespace. But that's generally a practice to be discouraged, so I'd very strongly recommend not to use any global variables. Move them so they are defined where needed, in this case in the main() function.
There are multiple problems with the way the input file is handled.
Line 53:
|
inFile.open(fileName, fLength);
|
should be
Function
loadFile()
has several issues. The parameter
animalCount
should be passed by reference, so that its new value can be passed back after the function ends.
The array
arrayOfCritters
is received as an input/output parameter, but it is never used. Instead a new local array
critters
is declared. Using eof() in a while condition is almost always wrong. Here it is made worse by the semicolon at the end of line 35, which means an infinite loop with no way to terminate.
In the body of the loop,
animalCount
is incremented each time an animal is read. But then on line 40 that valuable information is erased and reset to zero, and instead a value of 3 is assumed.
Back in function
main()
the use of
while (inFile)
doesn't make a great deal of sense, as the loop should execute until the user decides to quit.
Based upon the menu options, none of which mention reading the data from the file, it seems a better plan would be to read from the file at the start of main, before entering that loop.
Well, I'm sure that's a lot to take in. So here's the code with the issues I mentioned straightened out.
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
|
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
# define fLength 32
# define nLength 20
# define sLength 10
# define MAX 15
# define WIDTH 11
# define MAX_NUM_ANIMALS 15
struct Animals
{
char name[nLength];
char species[sLength];
int age;
};
void showArray (char name, char species, int age, int i);
void displayMenu();
void openFile(ifstream &inFile);
void loadFile(Animals arrayOfCritters[MAX_NUM_ANIMALS], int & animalCount, ifstream &inFile);
void loadFile(Animals arrayOfCritters[MAX_NUM_ANIMALS], int & animalCount, ifstream &inFile)
{
while (inFile >> arrayOfCritters[animalCount].name >> arrayOfCritters[animalCount].species >> arrayOfCritters[animalCount].age)
{
animalCount++;
}
for (int i = 0; i < animalCount; i++)
{
cout << arrayOfCritters[i].name << arrayOfCritters[i].species << arrayOfCritters[i].age << endl;
}
}
void openFile(ifstream &inFile)
{
char fileName[fLength];
cout << "Please enter the filename: ";
cin >> fileName;
cout << "\n\n";
inFile.open(fileName);
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()
{
ifstream inFile;
char name[nLength];
char species[sLength];
int age;
int animalCount = 0;
Animals arrayOfCritters[MAX_NUM_ANIMALS];
char choice = ' ';
openFile(inFile);
loadFile(arrayOfCritters, animalCount, inFile);
while (choice != 'Q' && choice != 'q')
{
displayMenu();
cout << "\nPlease enter your choice: ";
cin >> choice;
cout << "\n\n";
if (choice == '1')
{
}
else if (choice == '2')
{
}
else if (choice == '3')
{
}
else if (choice == '4')
{
}
else if (choice == '5')
{
}
else if (choice == '6')
{
}
else if (choice == 'q' || choice == 'Q')
{
cout << "Goodbye!\n\n";
system("pause");
return 0;
}
else if (choice < '1' || choice > '6')
{
cout << "Sorry, but that's not a valid option.\n";
cout << "Please choose again\n";
}
}
return 0;
}
|