C++/C++

Advertisement


Question
I have tried to develop a C++ program that read information of employees using project, I only managed to write it reading information of one employee. Can you help me to write reading 100 employees information and display them in tabular form. Here is what I have tried:
#ifndef employee_h_
#define employee_h_

class employee{
  int Salary_No; char Name[20];
  float Gross_Salary, Loan;
public:
  float Taxing();
  float NetPaying();
  void Display();
  void GetDetails();
  };
#endif

#include "employee.h"
#include <iostream.h>

void employee::GetDetails(){
cout<<"Enter employee's salary Number"<<endl;
cin>>Salary_No;
cout<<"Enter employee's name"<<endl;
cin>>Name;
cout<<"Enter employee's Gross Salary"<<endl;
cin>>Gross_Salary;
cout<<"Enter employee's loan"<<endl;
cin>>Loan;
}

float employee::Taxing(){
  float taxation;
  taxation = 0.2*Gross_Salary;
  return (taxation);
  }

float employee::NetPaying(){
  float netPaying;
  netPaying = Gross_Salary-Taxing()-Loan;
  return netPaying;
  }

void employee::Display(){
cout<<"Salary Number: "<<Salary_No<<endl;
cout<<"Name of the employee: "<<Name<<endl;
cout<<"Gross Salary: "<<Gross_Salary<<endl;
cout<<"Tax: "<<Taxing()<<endl;
cout<<"Loan: "<<Loan<<endl;
cout<<"Net Pay: "<<NetPaying()<<endl;

}

#include "employee.h"
#include <iostream.h>

int main(){

  employee detail;
  detail.GetDetails();
  detail.Display();

return 0;
}


Answer
a. have an array of 100 employees and a variable to hold the number of employees.
    
       enum { MAX_EMPLOYEES = 100 } ;
       employee employees_collection[ MAX_EMPLOYEES ] ;
       int number_of_employees = 0 ;

b. accept a positive number less than MAX_EMPLOYEES from the user,
set it to number_of_employees.

c. write a loop to accept data for number_of_employees employees.

       for( int i = 0 ; i < number_of_employees ; ++i )
         employees_collection[i].GetDetails() ;

d. modify employee::Display() to display information about the employee
in one line in fixed width columns.
use the header <iomanip>, and the manipulator std::setw to do this.
for a tutorial, see http://www.cprogramming.com/tutorial/iomanip.html

e. display the header containing captions ( "Salary Number",
"Name", "Gross Salary" etc.)

f. write another loop to display data for number_of_employees employees.

       for( int i = 0 ; i < number_of_employees ; ++i )
         employees_collection[i].Display() ;

and you are done.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.