[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 264
  • Last Modified:

Need help with a template function to output an array to a data file- urgent!

I need help with the outputData function.  The code is compiling, but when I check the file "integersOut.txt", it is empty.  Here is my code:

#ifndef H_listType
#define H_listType

#include <iostream>
using namespace std;

template <class elemType>
class listType
{
public:
    bool getData(char* filename);
        //loads array from file
   bool outputData(char* filename);
        //writes list to output file
    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 insert(const elemType& insertItem);
        //inserts item into list
   void removeAt(int location);
        //removes item at specified location from list
   void remove(const elemType& removeItem);
            // removes item from list
   int seqSearch(const elemType& item);
            //searches list for a specified 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>::outputData(char* filename)
{
      ofstream outfile(filename, ios::out);
      if(outfile.fail())
      {
            cerr<<"Error opening output file!\n";
            return false;
      }

      outfile.open(filename, ios::out);
      for(int i = 0; i < length; i++)
      outfile << list[i] << " " << endl;
                outfile.close();
      return true;
}

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


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>::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 <fstream>
#include "listType.h"
#include "myString.h"
using namespace std;

int main()
{
      int number;      
      listType<int> intList(100);      
      intList.getData("G:\\integers.txt");
                                                                        
      cout<<"The list of integers found in file integers.txt is: "<<endl;      
      intList.print();                                                      
      cout<<endl;                                                                  

      cout<<"After sorting, the list is: "<<endl;                                                                  
      intList.sort();                                                      
      intList.print();                                                      
      cout<<endl;
      cout<<endl;
      cout<<"Length of the list = " <<intList.getLength()<<endl;                              
      cout<<"Maximum size of the list = "      <<intList.getMaxSize()<<endl<<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\n";
      else
               cout<< "The value you entered is at location " << intList.seqSearch(number) << ".\n\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<<"After sorting, the new list is: "<<endl;                                                                  
      intList.sort();                                                      
      intList.print();
      cout<<endl;

      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();
      cout<<endl;
      cout<<endl;
      intList.outputData("G:\\integersOut.txt");
      
      return 0;
}
0
coririzzo
Asked:
coririzzo
1 Solution
 
SteHCommented:
You open file twice: one inside the constructor and once explicitly afterwards. The second one should fail which you don't check.
So remove the second opening statement:
bool listType<elemType>::outputData(char* filename)
{
     ofstream outfile(filename, ios::out);
     if(outfile.fail())
     {
          cerr<<"Error opening output file!\n";
          return false;
     }

     // outfile.open(filename, ios::out); // it is already open if you got here.
     for(int i = 0; i < length; i++)
     outfile << list[i] << " " << endl;
                outfile.close();
     return true;
}
0
 
coririzzoAuthor Commented:
I have really been working on this too long!  Big DUH!  Thanks!!!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now