Solved

Creating an array from input file

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

760 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

18 Experts available now in Live!

Get 1:1 Help Now