Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Creating an array from input file

Posted on 2003-11-16
5
Medium Priority
?
432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 140 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

610 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