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;
 }

The issue is that inFile isn't global, so every single function gets its own brand new version of inFile. The result is that you open it, then close it, then start trying to extract data from nothing. Either make inFile global, or pass it to each function that uses it and your problem should go away.

line 27 void openfile(ifstream &inFile) but the operation of that funtion is a bit lame because the is no logic only passing the stream object only to be asked for such operation.

I will advice you put a bit planing in your code.
1. char *getFile name(char *filename) for file name return char /*.
2. void openFile(char *fileName) open file with the getFile returned data.

one thing you must consider is that your file operation is not global. Therefore you will need to close the file each time and flush the data.
My best advice is to replan and maybe consider a class. With that your code will look more portable.
For line 73 does nothing.

Go back to the drawing board. I always draw before i code.

This article has been dead for over six months. Start a new discussion instead.