An array of objects...Within its class?

Hey yal,

first of many posts here. I'm at a crossroads in a project i'm working on at the moment, but i'm very near the end...

I'm trying to create an array of objects, which I will be sorting and performing calculations on; so I'll running procedures on individual objects as well as comparing the objects across the array of objects I'm trying to make.

I thought I just about had it...but the function I'm trying to run a sort in cannot see the array; or says the object elements are undeclared, depending on a few issues...

--I tried having the function to sort inside the class but I am confused how to make an array of elements of the same object within its class

--I initially had a very messy main...I held the array of objects in it, and tried creating another function, not inside the class; however, the values of the object were undeclared--I tried correcting this by opening the private class data types to public...but nothing

Performing calculations on each individual object hasn't been an issue; however, when trying to sort or compare then array is undeclared or data types are undeclared...

I'm guessing its a scope issue, but haven't been able to resolve it wherever I move my code...


Last edited on
This post isn't a question, it just sort of rambles. Can you try to shorten what you are asking for?
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;
class payRoll{
public:
int ID;
string LAST;
string FIRST;
double WAGE;
double HOURS;
double DEDUCTION;
double aGROSS;
char DEPT;
char STATUS;
char DEPEND;
char INS;
payRoll();
void print();
};
payRoll::payRoll(){}
void payRoll::print(){
cout<<setprecision(2)<<setiosflags(ios::fixed)<<setiosflags(ios::showpoint)
<<ID<<setw(12)<<LAST<<setw(12)<<FIRST<<setw(10)
<<WAGE<<setw(10)<<HOURS<<setw(10)<<DEPT<<"\n";
}
string& LTrim(string& str){
int i;
for(i=0;str[i]!=0 && str[i]<=32;)
i++;
str=str.substr(i,str.length()-i);
return str;
}
int main(int argc, char** argv){
payRoll employees[50];
int pos;
string line;
ifstream myfile (*(argv+1));

if (myfile.is_open())
{
for(int i=0; i<43;i++){
getline (myfile,line);

employees[i].ID= atoi(line.substr(0,pos=line.find(" ")).c_str());
line.erase(0,pos);
line= LTrim(line);

employees[i].LAST= line.substr(0,pos=line.find(" "));
line.erase(0,pos);
line= LTrim(line);

employees[i].FIRST= line.substr(0,pos=line.find(" "));
line.erase(0,pos);
line= LTrim(line);

employees[i].WAGE= atof(line.substr(0,pos=line.find(" ")).c_str());
line.erase(0,pos);
line= LTrim(line);

employees[i].HOURS= atof(line.substr(0,pos=line.find(" ")).c_str());
line.erase(0,pos);
line= LTrim(line);

employees[i].DEPT=line.at(0);
//employees[i].print();
}
myfile.close();
}
ifstream myfile2 (*(argv+2));
if(myfile2.is_open())
{
int tempID;
for(int i=0; i<43;i++){
getline (myfile2,line);

tempID= atoi(line.substr(0,pos=line.find(" ")).c_str());
line.erase(0,pos);
line= LTrim(line);

for(int j=0;j<43; j++){
if(employees[j].ID==tempID){
employees[j].WAGE*=(1+(atof(line.substr(0,pos=line.find(" ")).c_str())/100));
line.erase(0,pos);
line= LTrim(line);
cout<<employees[j].WAGE<<"\n";

employees[j].STATUS= line.at(0);
line.erase(0,1);
line= LTrim(line);

employees[j].DEPEND= atoi(line.substr(0,1).c_str());
line.erase(0,1);
line= LTrim(line);

employees[j].INS= line.at(0);
line.erase(0,1);
line= LTrim(line);
}}}}
else cout << "Unable to open file";
return 0;
}


ok well here is what i'm looking at... and at the moment its just barely OO. I'm trying to sort this with...

void sortDept(payRoll *emp){
payRoll temp;
for(int j=0; j<43; j++){
for(int i=0; i<43-1; i++){
if((emp+j)->DEPT>(emp+i+1)->DEPT){
temp=*(emp+i);
*(emp+i)=*(emp+i+1);
*(emp+i)=temp;
}}}}


but i've been running into problems... in this case DEPT is undeclared

i've tried to make revisions to the code but all have fallen short
Last edited on
ok i edited my last post to include the code that i originally ran into to trouble with...

i've made many revisions but plz fire away

why is is DEPT undeclared in my sort, when DEPT is public in my class?

or how can i store my employees differently so that I can sort them by DEPT(5 different char's) and also by FIRST and LAST name
ok so i found the issue "(emp+i)->" instead of *(emp+i).DEPT

still dont' sort 'doh
even if i do the same sort for last name


payRoll temp;
for(int j=0; j<43; j++){
for(int i=0; i<43-1; i++){
if(strcmp(((emp+j))->LAST.c_str(),((emp+i+1)->LAST).c_str())){
temp=*(emp+i);
*(emp+i)=*(emp+i+1);
*(emp+i)=temp;
}}}

i don't get ANY sorting, just the same mumbo jumbo
Topic archived. No new replies allowed.