Link to home
Start Free TrialLog in
Avatar of yllee
yllee

asked on

round up

#include <iostream.h>
#include <string.h>

class Employee
{
      private :
            char *name;
            int age;
            int YearsOfService;
            float Salary;

      public :
            Employee();
            void setEmployeeDetail();
            void setEmployeeName(char*);
            void setAge(int);
            void setYearsOfService(int);
            void setSalary(float);
            void display();
};

Employee::Employee()
{
  name = "";
  age = 0;
  YearsOfService = 0;
  Salary = 0;
}


void Employee::setEmployeeDetail()
{
      const int Max = 80;
      char eName[Max];
      int eAge, eYOS;
      float eSalary;

      cout << "Enter employee name : ";
      cin.getline(eName, Max);

      cout << "Enter age : ";
      cin >> eAge;

      cout << "Enter salary : $";
      cin >> eSalary;

      cout << "Enter Years of service : ";
      cin >> eYOS;
      cin.ignore();
      cout << endl;

      setEmployeeName(eName);
      setAge(eAge);
      setSalary(eSalary);
      setYearsOfService(eYOS);
}

void Employee::setEmployeeName(char *eName)
{
      name = new char[strlen (eName) + 1];
      strcpy(name, eName);
}

void Employee::setAge(int x)
{
      age = x;
}

void Employee::setYearsOfService(int x)
{
      YearsOfService = x;
}

void Employee::setSalary(float x)
{
      Salary = x;
}

void Employee::display()
{
      cout << endl;
      cout << "Employee name : " << name;
      cout << endl;
      cout << "Age : " << age;
      cout << endl;
      cout << "Salary : $" << Salary;
      cout << endl;
      cout << "Year of service : " << YearsOfService;
      cout << endl;
      cout << endl;
}


void main()
{
      int i;
      Employee erecord[2];

      for (i = 0; i < 2; i++)
            erecord[i].setEmployeeDetail();

      for (i = 0; i < 2; i++)
            erecord[i].display();
}

How can I rounded the salary to the nearest 1,000?
ASKER CERTIFIED SOLUTION
Avatar of codeten
codeten

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jasonclarke
jasonclarke

codeten, your code divides the salary by 1000! I'm glad I don't work for your company :).  Also, there is no rounding since the arithmetic is floating point.

try the following:

#include <math.h>
....
Salary = floor(Salary/1000+0.5)*1000;

BTW, you put the line in the SetSalary method just after Salary = x; and put the #include with the other #includes.
It would be nice if the accepted answer was correct!
Sorry guys, I didn't really look at the code. I was rushed. Its just a general rounding algorithm I jotted down. And yes, jc, I forgot to *1000.