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?
ylleeAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
codetenConnect With a Mentor Commented:
Add 500 to the salary and divide by 1000

i.e.

Salary = (Salary + 500)/1000
0
 
jasonclarkeCommented:
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;

0
 
jasonclarkeCommented:
BTW, you put the line in the SetSalary method just after Salary = x; and put the #include with the other #includes.
0
 
jasonclarkeCommented:
It would be nice if the accepted answer was correct!
0
 
codetenCommented:
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.
0
All Courses

From novice to tech pro — start learning today.