This is the output i want outputted after information is inputted. instead of my
code that skips the 1st name info.
Day of Week: Wednesday
No of painters: 2
Name 1: Jack
Name 2: Zee
---------------------------------------------
Day of Week: Thursday
No of painters: 1
Name: Zee
********employee wages**********
Name: Zee
Hrs works: 6 at R5.75 an hr. Total wages: R34.50
Day of week:Wednesday, Thursday
-----------------------------------------------
Name: Jack
Hrs works: 3 at R5.75 an hr. Total wages: R17.25
Day of week:Wednesday
#include <iostream>
#include <string>
#include <iomanip>
#include <vector>
usingnamespace std;
class bBuilding
{
private:
staticconst size_t NR_BUILINGS=2;
int addressN[NR_BUILINGS];
string addressS[NR_BUILINGS];
string dOWeek[NR_BUILINGS];
int noPainters[NR_BUILINGS];
string paintersN[NR_BUILINGS];
string choice[NR_BUILINGS];
int numHrs[NR_BUILINGS];
public:
void getPaintersDetails()
{
for (size_t count = 0; count<NR_BUILINGS; ++count)
{
cout<<"Address number then Street name ";
cin>>addressN[count]; cin.ignore(); getline(cin, addressS[count]);
cout<<"Please enter Day of Week serviced: ";
getline(cin,dOWeek[count]);
cout<<"Number of painters who worked on that day: ";
cin>>noPainters[count];
cin.ignore();
for (int i=0; i<noPainters[count]; i++)
{ cout << "Name of the Painter " << i+1 << "? ";
cin>>paintersN[count];
cin.ignore();}
numHrs[count]=3;
cout<<"--------------------------------------------------------"<<endl;
}
}
void wages()
{cout<<"*************************"<<setw(16)<<"ALL EMPLOYEES WAGES"<<"**************************"<<endl;
for( size_t count = 0; count < NR_BUILINGS; ++count )
{
const string& name = paintersN[count] ;
if( !name.empty() ) // if this is a name that was not already processed
{
cout << "Name: " << name << '\n' ;
int paintersHr=numHrs[count];
//iterate through the array to see if name appears again
for( size_t i = count+1; i < NR_BUILINGS; ++i )
if( paintersN[i] == name )
paintersHr += numHrs[i] ;
double price = 5.75;
cout << "Hours worked: " << paintersHr <<" at R5.75 an hr. "<< " Total wages: "
<<fixed <<setprecision(2) <<"R"<<paintersHr * price << '\n' ;
cout << "Day of week: " << dOWeek[count] ;
// iterate through the array to see if this name appears again
for( size_t i = count+1; i < NR_BUILINGS; ++i ) if( paintersN[i] == name )
{
cout << ' '<<"," << dOWeek[i] ;
paintersN[i] = "" ; // we are done with this name, we don't want to process it again
}
cout << "\n\n" ;
cout<<"----------------------------------------------------------------"<<endl;
}
}
}
};
int main()
{
bBuilding e1;
e1.getPaintersDetails();
e1.wages();
}
Line 36: You can only store one painter in paintersN[count].
You're misusing your class. Since everything in your class occurs NR_BUILINGS times, it should be the instance at line 82 that occurs NR_BUILINGS times. Your member variables should occur once (except painterN) and not occur NR_BUILINGS times. Each instance of bBuilding should refer to ONE building or job.
paintersN should occur MAX_PAINTERS times (whatever that value is).
1 2 3 4 5 6 7 8 9 10 11 12
class bBuilding
{ int addressN;
string addressS;
string dOWeek;
int noPainters;
string paintersN[MAX_PAINTERS];
string choice;
int numHrs;
public:
void getPaintersDetails();
void wages();
};
You're missing the point. Your class should represent ONE building or job, not a collection of such. You want a collection of buildings or jobs, use a std::vector or an array of instances of your class.