Solved

Creating an array from input file

Posted on 2003-11-16
5
391 Views
Last Modified: 2010-04-01
I am trying to build an array using the following function:

template <class T>
void makeArray(const string &inFile, T *& A)
{
      int arraySize;

      ifstream input;
      input.open(inFile.c_str());
            
      if (!input.is_open())                     //is inFile open?
      {
            cout << "Error opening input file";
            exit(1);                               //exits if no inFile
      }

      input >> arraySize;
      A=new T[arraySize];

      //form arrays
      for (int i=0; i<arraySize; i++)
      {
            input >> A[i];
      }

      input.close();
}      

to call it from main I use:

      int* unsortedInts;
      
      int* unsortedStrs;
      
      makeArray(inFile, unsortedInts);
      makeArray(inFile, unsortedStrs);

but then it read the file in as the same array.  I want the file to read in with one as strings and one as ints.  How would I change my code to allow this?  I know I have to tell the function what type to  use for T, but I am stuck on how.  Thanks,

Ant_Dogg
0
Comment
Question by:Ant_Dogg
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:n_fortynine
ID: 9760082
why do you have int* unsortedStrs when you want T to be string? Change that to string* unsortedStrs;
0
 
LVL 30

Expert Comment

by:Axter
ID: 9760121
unsortedStrs should be of type std::string if you want a string array.
0
 

Author Comment

by:Ant_Dogg
ID: 9760163
Alright, then there is a part two to my question.  After the array is created, I want to send it to a sorting algorithm such as:

template <class T>
void quickSort(T A[], int left, int right)
{
      int i, j, temp, count=0;
      T pivot = A[right];

      if (left < right)
      {
            countPlus();
            i = left;            
            j = right;

            do
            {
                  while (A[i]<pivot)
                  {
                        i++;
                        countPlus();
                  }
                  countPlus();
                  while (pivot<A[j])
                  {
                        j--;
                        countPlus();
                  }
                  countPlus();

                  if (i<=j)
                  {
                        temp = A[i];
                        A[i] = A[j];
                        A[j] = temp;

                        i++;
                        j--;
                        countPlus();
                  }
                  else{ countPlus();}
            }
            while (i <= j);
                  
            // partitions formed

            // left partition boudaries: left and j
            quickSort(A, left, j);

            // right partition boundaries: i and right
            quickSort(A, i, right);
      }
      else{ countPlus();}
}

but it then gives me an error at compile  that says:

cannot convert `basic_string<char,
string_char_traits<char>, __default_alloc_template<true, 0> >' to `int'
in assignment

what do I need to change in my quickSort to make it work correctly?
0
 
LVL 30

Expert Comment

by:Axter
ID: 9760168
Example code:

template <class T>
int makeArray(const string &inFile, T *& A)
{
    int arraySize;
      
    ifstream input;
    input.open(inFile.c_str());
      
    if (!input.is_open())                     //is inFile open?
    {
            cout << "Error opening input file";
            exit(1);                               //exits if no inFile
    }
      
    input >> arraySize;
    A=new T[arraySize];
      
    //form arrays
    for (int i=0; i<arraySize; i++)
    {
            input >> A[i];
    }

    input.close();
      return arraySize;
}    

int main(int argc, char* argv[])
{
      const std::string inFile = "test.dat";
      int* unsortedInts;
   
      std::string* unsortedStrs;
      
    int int_size = makeArray(inFile, unsortedInts);
    int str_size = makeArray(inFile, unsortedStrs);

      int i;
      for (i = 0; i < int_size;++i) cout << unsortedInts[i] << endl;
      cout << endl << endl;
      
      for (i = 0; i < str_size;++i) cout << unsortedStrs[i] << endl;
      cout << endl << endl;
      
      delete [] unsortedInts;
      delete [] unsortedStrs;
      
      system("pause");
      return 0;
}

0
 
LVL 4

Accepted Solution

by:
n_fortynine earned 35 total points
ID: 9760177
You have temp declared as int above.
>>temp = A[i];
>>A[i] = A[j];
>>A[j] = temp;
So to make it work. Declare temp as:
T temp;
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now