Solved

Creating an array from input file

Posted on 2003-11-16
5
401 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

776 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