Need help to add a function to a template to load an array from a data file

My program works fine when the user enters the data to load the array.  Here is my code:

//Header file listType.h

#ifndef H_listType
#define H_listType

#include <iostream>
using namespace std;

template <class elemType>
class listType
{
public:
      
    bool isEmpty();
        //Returns true if the list is empty, false otherwise
    bool isFull();
        //Returns true if the list is full, false otherwise
    int getLength();
        //Returns the length of the list
    int getMaxSize();
        //Returns the maximum number of elements that can be stored in the list
    void sort();  
          //Sorts the list
   void print() const;
          //Outputs the elements of the list
    void insertAt(int location, const elemType& insertItem);
        //Post: list[location] = item; length++;
        //If the location is out of range, the program is aborted
      
   void insert(const elemType& insertItem);
      
   void removeAt(int location);

   void remove(const elemType& removeItem);

   int seqSearch(const elemType& item);

    listType(int listSize = 50);
         //constructor
          
   ~listType();
          //destructor
          
private:
    int maxSize; //maximum number that can be stored in the list
    int length;  //number of elements in the list
    elemType *list; //pointer to the array that holds the list elements
};

template<class elemType>
bool listType<elemType>::isEmpty()
{
      return (length == 0);
}

template<class elemType>
bool listType<elemType>::isFull()
{
      return (length == maxSize);
}

template<class elemType>
int listType<elemType>::getLength()
{
      return length;
}

template<class elemType>
int listType<elemType>::getMaxSize()
{
      return maxSize;
}

template<class elemType>
listType<elemType>::listType(int listSize)
{
      maxSize = listSize;
      length = 0;
      list = new elemType[maxSize];
}

template<class elemType>
listType<elemType>::~listType()  //destructor
{
      delete [] list;
}

template<class elemType>
void listType<elemType>::sort()   //selection sort
{
      int i, j;
      int min;
      elemType  temp;

      for(i = 0; i <length; i++)
      {
            min = i;
            for(j = i+1; j < length; ++j)
               if(list[j] < list[min])
                  min = j;
            temp = list[i];
            list[i] = list[min];
            list[min] = temp;
      }
}

template<class elemType>
void listType<elemType>::print() const
{
      int i;
      for(i = 0; i < length; ++i)
            cout<<list[i]<<"  ";
      cout<<endl;
}


template<class elemType>
void listType<elemType>::insertAt(int location, const elemType& insertItem)
{
      int i;
      if(location < 0 || location >= maxSize)
            cout<<"The location of the item to be inserted is out of range"<<endl;
      else
            if(length >= maxSize)
                  cout<<"Cannot insert in a full list"<<endl;
            else
            {
                  for(i = length; i > location; i--)
                        list[i] = list[i-1];
                  list[location] = insertItem;
           
                  length++;
            }
}

template<class elemType>
void listType<elemType>::insert(const elemType& insertItem)
{
      if (length >= maxSize)
            cout<<"Cannot insert in a full list."<<endl;
      else
            list[length] = insertItem;
            length++;
}

template<class elemType>
void listType<elemType>::removeAt(int location)
{
      int i;

      if(location < 0 || location >= length)
          cout<<"The location of the item to be removed "
                  <<"is out of range"<<endl;
      else
      {
               for(i = location; i < length - 1; i++)
                   list[i] = list[i+1];

            length--;
      }
}

template<class elemType>
void listType<elemType>::remove(const elemType& removeItem)
{
      int loc;
      if(length == 0)
            cout <<"Cannot delete from an empty list."<<endl;
      else
      {
            loc = seqSearch(removeItem);

            if(loc != -1)
                  removeAt(loc);
            else
                  cout<<"The item to be deleted is not in the list."<<endl;
      }
}

template<class elemType>
int listType<elemType>::seqSearch(const elemType& item)
{
      int loc;
      bool found = false;

      for(loc = 0; loc < length; loc++)
            if(list[loc] == item)
            {
                  found = true;
                  break;
            }
      if(found)
            return loc;
      else
            return -1;
}


#endif
________________________

#include <iostream>
#include "listType.h"
using namespace std;

int main()
{
      listType<int> intList(100);                                          
      
      int number;                                                                  
                cout<<"Line 3: Enter numbers, then -999 to quit:" <<endl;                                                      cin>>number;                                                            
      while(number != -999)                                                
      {
            intList.insert(number);                                          
            cin>>number;                                                      
      }                  
      cout<<endl;                                                                  
      cout<<"The list you entered is: "<<endl;      
      intList.print();                                                      
      cout<<endl;                                                                  

      cout<<"After sorting, the list is: "<<endl;                                                                  intList.sort();                                                      
      intList.print();                                                      
      cout<<endl;                                                                  
      cout<<"Length of the list = " <<intList.getLength()<<endl;                              
      cout<<"Maximum size of the list = " <<intList.getMaxSize()<<endl;                              
      cout<<"Enter the value you would like to search for:"<<endl;
      cin>>number;
      if (intList.seqSearch(number) == -1)
            cout<<"The value you entered is not in the list.\n";
      else
                                cout<< "The value you entered is at location " << intList.seqSearch(number) << ".\n";

      cout<< "Enter the item you would like to add to the list:"<<endl;
      cin>>number;
      intList.insert(number);
      cout<<"The new list is: "<<endl;
      intList.print();

      cout<< "Enter the item you would like to remove from the list:"<<endl;
      cin>>number;
      intList.remove(number);
      cout<<"The new list is: "<<endl;
      intList.print();
      

      return 0;
}
_________________________________

Now I need to add a function to load the array from a data file.  Here is what I tried:

template<class elemType>
bool listType<elemType>::getData(elemType list)
{
      int index;
      int max;
      ifstream infile("G:\\integers.txt", ios::in);
      if(infile.fail())
      {
            cerr<<"Error opening input file!\n";
            return false;
      }
      for (max = 0, index = 0; !infile.eof(); index++)
      {
            infile>>list[index].number;
            max++;
      }
      infile.close();
      return true;
}

Added this line to main program and removed code for user entry of data:

intList.getData(list);

The compiler says that list is an undeclared identifier.

Please help!!!!
coririzzoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hamed ZaghaghiProgrammerCommented:
hi,
why you want to use a list in function `getData'?
you not need to do, 'cuse your list is a private mmber of intist obj, and so you can use `list' in your code with out getting from function argument.
and wll e safe that you use member function `insert', for range of array list.

good luck
0
Hamed ZaghaghiProgrammerCommented:
you can use this

template<class elemType>
bool listType<elemType>::getData(char* filename)
{
     elemType temp;

     ifstream infile(filename, ios::in);
     if(infile.fail())
     {
          cerr<<"Error opening input file!\n";
          return false;
     }

     while(!infile.eof())
     {
          infile>>temp;
          insert(temp);
     }
     infile.close();
     return true;
}

good luck;
0
coririzzoAuthor Commented:
OK, I understand your code except for char*.   I added the function to my program and substituted "G:\\integers.txt" for filename, but it is not working.  Also, how do you call this function in main?  
Thanks!
0
jkrCommented:
Well, 1st of all, there's no 'getData()' in your class declaration and 'list' is already a member of your class. In 'main()', you'd call that function like

     listType<int> intList(100);

     intList.getData("G:\\integers.txt");

But, there's a serious flaw in the above function, since it does not check whether the list is large enought to store all the data. That should be

template<class elemType>
bool listType<elemType>::getData(char* filename)
{
    elemType temp;
     int i = 0;

    ifstream infile(filename, ios::in);
    if(infile.fail())
    {
         cerr<<"Error opening input file!\n";
         return false;
    }

    while(!infile.eof() && i < maxSize)
    {
         infile>>temp;
         insert(temp);

         ++i;
    }
    infile.close();
    return true;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.