Solved

Creating an array from input file

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

790 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