Hello everyone!
I'm fairly new to C++ and I have hit a serious road block....I cannot for the life of me get my program to fill a structure from a file and print the array correctly. I don't know what to do :( It prints a series of numbers because for some reason it's not taking the data from the text file.
Here's the data I'm using:
Goofy dog 8
Minnie mouse 22
Daisy duck 18
Mickey mouse 25
Pluto dog 10
Donald duck 21
Here's my code so far:
(there's some additional values that will be used later in the program)
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
using namespace std;
// Constant Values
#define WIDTH 16
#define FILENAME 32
#define MAXANIMALS 15
#define MAXNAME 20
#define MAXSPECIES 10
// Structure
struct Animals
{
char name [MAXNAME];
char species [MAXSPECIES];
int age;
};
// Functions
void openFile()
{
ifstream inFile;
char fileName[FILENAME];
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 populate(Animals arrayOfCritters[MAXANIMALS])
{
ifstream inFile;
for( int idx = 0; idx < MAXANIMALS; ++idx )
{
inFile >> arrayOfCritters[idx].name >> arrayOfCritters[idx].species >> arrayOfCritters[idx].age;
}
}
void optionOne(Animals arrayOfCritters[MAXANIMALS])
{
cout << endl
<< "Name Species Age" << endl
<< "--------- --------- -------" << endl;
cout.setf( ios::left );
for( int idx = 0; idx < MAXANIMALS; ++idx )
{
cout << setw(WIDTH) << arrayOfCritters[idx].name;
cout << setw(WIDTH) << arrayOfCritters[idx].species;
cout << arrayOfCritters[idx].age << endl;
}
cout.unsetf( ios::left );
cout << endl;
}
int main()
{
ifstream inFile;
Animals arrayOfCritters[MAXANIMALS];
openFile();
populate(arrayOfCritters);
optionOne(arrayOfCritters);
system("pause");
return 0;
}