Hello,
I have a program where I am using an object array that I declare from a class. I am trying to store information from a text file into this array. I will then be doing other items like searching the array and displaying it into another text file. I was wondering if you can look at the code below and let me know what I am doing wrong.

int main()
{

    int loop=0; //Index of array
    int loop2;
    string line; //The line taken from the *.txt source
    const int num = 6;
    myClass classObj[num];  //Define an array of objects

    ifstream myfile ("myTextFile.txt"); //To read from the *.txt File

    if (myfile.is_open()) //Checking if the file can be opened
    {
        while (! myfile.eof() ) //Runs while the file is NOT at the end
        {
            getline (myfile,line); //Gets a single line from example.txt
            classObj[loop]=line; //Saves that line in the array
            loop++; //Does an increment to the variable 'loop'
        }
        myfile.close(); //Closes the file
    }
    else cout << "Unable to open file"<<endl; //Gives that sentence if the file can't be opened
    for(loop2=0;loop2<=loop;loop2++) //For loop make to cout the lines stored
    cout<<classObj[loop2]<<endl;    //inside of the variable 'array'
  return 0;
}

I am getting the following error
error: no match for 'operating=' in 'classObj[loop] = '

the text file will look something to the effect of what is listed below. There will be exactly 6 lines in the file

hi my name # is fred

Call new on your object when adding it to your array.

sorry I am confused...I do not want to use a vector or a pointer if at all possible. I have added the following line.

myClass   = new string classObj[num]

won't I need to convert the array to a int.

It would be more like:

classObj[num] = new myClass(line);

IF your class has a constructor that takes a string as its parameter.

Can you post the definition of your class?
That will really dictate what should be done.
Also, does getline() take a std::string as its second parameter?

Does this code actually compile?

I actually thought it might be easier to go and see the variables by reference. So I modified my code to do this

int main()
{
    const int arraySize = 5;
    int indexOfArray = 0;
    Book bookList[arraySize];
    double tempPrice;//temporary stores price
    string tempStr;//temporary stores author, title
    string test;


    fstream fileIn( "file.txt" );

            while ( !fileIn.eof( ))
        {
            getline(fileIn,tempStr);
           test =  bookList[indexOfArray].getName(tempStr);


           // getline(fileIn,tempStr);
           // bookList[indexOfArray].setTitle(tempStr);

           // fileIn >> tempPrice;
           // bookList[indexOfArray].setPrice(tempPrice);
                cout << indexOfArray;
                cout << arraySize;
            if ( indexOfArray < arraySize )//shifting array index while not exceeding array size
            {
                indexOfArray++;
                cout << test;
            }
        }
        //system("PAUSE");

    fileIn.close();
    for (int i = 0; i < arraySize; i++)
    {
        cout << test << endl;
    }
return 0;
}

hopefully that helps and I can still post my header file if you would like to see it.

Right now I am in an infinite loops of 5

I need to see the definition of the class Book.
Once again, does this code compile???

Sorry I didn't answer the compile question. It does compile 100% without any errors when I am passing variables by reference.

Here is the header file that i am using for

class Employee
{
  private:
    int id;             // employee ID
    string name;        // employee name
    double hourlyPay;   // pay per hour
    int numDeps;        // number of dependents
    int type;           // employee type

  public:
    Employee( int initId=0, string initName="",
              double initHourlyPay=0.0,
              int initNumDeps=0, int initType=0 );  // Constructor


    bool set(int newId, string newName, double newHourlyPay,
             int newNumDeps, int newType);
    int getID();
    string getName(string &name);
    double getHourlyPay();
    int getNumDeps();
    int getType();
};


Employee::Employee( int initId, string initName,
                    double initHourlyPay,
                    int initNumDeps, int initType )
{
  bool status = set( initId, initName, initHourlyPay,
                     initNumDeps, initType );


  if ( !status )
  {
    id = 0;
    name = "";
    hourlyPay = 0.0;
    numDeps = 0;
    type = 0;
  }
}


bool Employee::set( int newId, string newName, double newHourlyPay,
                                 int newNumDeps, int newType )
{
  bool status = false;


  if ( newId > 0 && newHourlyPay > 0 && newNumDeps >= 0 &&
       newType >= 0 && newType <= 1 )
  {
    status = true;
    id = newId;
    name = newName;
    hourlyPay = newHourlyPay;
    numDeps = newNumDeps;
    type = newType;
  }
  return status;
}


int Employee::getID()
{
return id;
}
string Employee::getName(string &name)
{
return name;
}
double Employee::getHourlyPay()
{
return hourlyPay;
}
int Employee::getNumDeps()
{
return numDeps;
}
int Employee::getType()
{
return type;
}

the book and employee header files are the exact same I just changed the names on them

So, a book has a number of departments and hourly pay?

OK. What are the first three lines you are reading from file.txt?

the first 3 lines of the text file are:
5 Christine Kim # 30.00 3 1

15 Ray Allrich # 10.25 0 0

16 Adrian Bailey # 12.50 0 0

OK I understand, now:

Try this:

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>

using namespace std;

class Employee
{
  private:
    int id;             // employee ID
    string name;        // employee name
    double hourlyPay;   // pay per hour
    int numDeps;        // number of dependents
    int emptype;           // employee type

public:
   Employee()
   {
      id=0;
      name="";
      hourlyPay = 0.0;
      numDeps = 0;
      emptype = 0;
   }

   Employee(string strData)
   {
      string strTemp;
      istringstream iss(strData);
      iss >> id;
      iss >> strTemp;
      iss >> name;
      name = strTemp + " " + name;
      iss >> strTemp;
      iss >> hourlyPay;
      iss >> numDeps;
      iss >> emptype;
   }
};

int main(void)
{
   ifstream fileIn("Employees.txt");
   string strData= "";
   vector<Employee> lstOfEmployees;

   while(!fileIn.eof())
   {
      getline(fileIn, strData);
      if(!(strData.length() > 5))
      {
         continue;
      }

      lstOfEmployees.push_back(Employee(strData));
   }
   
   fileIn.close();

   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.