Alright, this isn't actually homework. I'm going back to school so I figured I would brush up on some programming before I go. I'll be taking the second course in a three course C++ programming sequence (starts with classes, recursion, etc.), so nothing too advanced yet. I wrote this program to review classes and input/output streams. It should read in three numerical values from a file, do some basic manipulations, then output everything. I was wondering if anyone would like to proofread it and give me some suggestions as to how it could be better? Thanks. Also, a minor problem I'm having is that the output isn't showing two digits of precision.
header file:
#include <iostream>
#include <string>
using namespace std;
#ifndef SALARY_H
#define SALARY_H
const double MINRAISE = .05;
const int MINSALARY = 15000;
const double MIDRAISE = .07;
const double MAXRAISE = .1;
const int MAXSALARY = 50000;
const int WEEKS = 52;
const int MONTHS = 12;
const int MIN = 0;
const int DEP = 4;
const int MAX_ID= 999;
class Salary
{
public: Salary();
Salary(int dep, int ID, double sal);
const int getDepNum();
const int getWorkerID();
const double getYearlySalary();
const string getDepartment();
void setDepNum(int x);
void setWorkerID(int x);
void setYearlySalary(double x);
void setDepartment(int x);
const double newSalary();
const double monthlySalary();
const double weeklySalary();
private: int depNum, workerID;
double yearlySalary;
string department;
friend istream& operator >> (istream &in, Salary &x);
friend ostream& operator << (ostream &out, Salary &x);
};
#endif
Implementation File
#include <iostream>
#include <string>
#include "salary_header.h"
using namespace std;
Salary::Salary(int dep, int ID, double sal)
{
depNum = dep;
workerID = ID;
yearlySalary = sal;
}
const int Salary::getDepNum()
{
return depNum;
}
const int Salary::getWorkerID()
{
return workerID;
}
const double Salary::getYearlySalary()
{
return yearlySalary;
}
const string Salary::getDepartment()
{
return department;
}
void Salary::setDepNum(int x)
{
if( (x > MIN) && (x <= DEP))
depNum = x;
else
depNum = 1;
}
void Salary::setWorkerID(int x)
{
if( (x > MIN) && (x <= MAX_ID))
workerID = x;
else
workerID = MIN;
}
void Salary::setYearlySalary(double x)
{
if( (x > MIN))
yearlySalary = x;
else
yearlySalary = MIN;
}
void Salary::setDepartment(int x)
{
if(x==1)
department = "Computer Science";
else if(x==2)
department = "Business Administration";
else if(x==3)
department = "Accounting";
else if(x==4)
department = "Electrical Engineering";
else
department = "Invalid Department";
}
const double Salary::newSalary()
{
if(yearlySalary < MINSALARY)
return ((yearlySalary*MINRAISE) + yearlySalary);
else if( (yearlySalary >= MINSALARY)&&(yearlySalary < MAXSALARY))
return ((yearlySalary*MIDRAISE) + yearlySalary);
else
return ((yearlySalary*MAXRAISE) + yearlySalary);
}
const double Salary::monthlySalary()
{
return (newSalary()/MONTHS);
}
const double Salary::weeklySalary()
{
return (newSalary()/WEEKS);
}
ostream& operator << (ostream &out, Salary &x)
{
cout.setf(ios::fixed);
cout.precision(2);
cout.showpoint;
x.setDepartment(x.getDepNum());
out << "Employee ID: " << x.getWorkerID() << endl;
out << "Department: " << x.getDepartment() << endl;
out << "Old Salary: " << x.getYearlySalary() << endl;
out << "New Salary: " << x.newSalary() << endl;
out << "New Monthly Salary: " << x.monthlySalary() << endl;
out << "New Weekly Salary: " << x.weeklySalary() << endl << endl << endl;
return out;
}
istream& operator >> (istream &in, Salary &x)
{
in >> x.depNum >> x.workerID >> x.yearlySalary;
return in;
}
Driver
#include <iostream>
#include <fstream>
#include <string>
#include "salary_header.h"
using namespace std;
void readWrite(Salary& salary);
int main()
{
Salary salary(0,0,0);
int userDep(0), userID(0);
double userSalary(0);
readWrite(salary);
system("PAUSE");
return 0;
}
void readWrite(Salary& salary)
{
ifstream in;
ofstream out;
in.open("data.txt");
out.open("OutputData.txt");
if(in.fail())
{
cout << "File opening failed";
exit(1);
}
while (!(in.eof()))
{
in >> salary;
out << salary;
}
in.close();
out.close();
}