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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

codetenCommented:
Add 500 to the salary and divide by 1000

i.e.

Salary = (Salary + 500)/1000
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.