/*
Salary Increase Calculator
Name: Reagan Fischer
CSC160.1N2
This program reads names, salaries, and proposed increases in pay from a hard-coded data file called Ch3_Ex6Data.txt.
It then writes calculated salary increases to Ch3_Ex6Output.dat.
*/
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
double salary_calc(double CurrentSalary, double IncreaseRate); // Declare function for compiler to avoid errors.
int main() {
ifstream SalaryData; // Define the filestream object that will contain our data.
ofstream ModifiedData; // Define the filestream object that we will output to.
// Define strings to hold the entire current line, current name, the split name, and the
// current salary/increase.
string SalaryData_Line;
string Name;
string FirstName;
string LastName;
string Number;
// Define doubles to hold the current salary, the increase, and the final
// salary.
double Salary = 0.0;
double Increase = 0.0;
double OutputSalary = 0.0;
// Define size_t variables to hold the index of the char in our string where
// the salary data starts and where the salary increase data starts.
size_t IndexOfSalary = 0;
size_t IndexOfIncrease = 0;
size_t IndexOfName = 0;
// Open our files for reading and writing.
SalaryData.open("Ch3_Ex6Data.txt");
ModifiedData.open("Ch3_Ex6Output.dat");
// Set our output file to write floating point numbers with the decimal always
// showing and with a fixed precision of two.
ModifiedData << fixed << showpoint << setprecision(2);
// Begin reading from our file handle in a loop and saving , terminating when
// EOL is reached.
while (getline(SalaryData, SalaryData_Line)) {
IndexOfSalary = SalaryData_Line.find_first_of("0123456789"); // Read from salarydata_line and return the index of the first number found.
// Use this number to split the string into name and number.
Name = SalaryData_Line.substr(0, IndexOfSalary);
Number = SalaryData_Line.substr(IndexOfSalary);
IndexOfName = SalaryData_Line.find_first_of(' '); // Read from name and return the index of the first space found.
// Use this number to split the string into firstname and lastname.
FirstName = Name.substr(0, IndexOfName);
LastName = Name.substr(IndexOfName);
IndexOfIncrease = Number.find_first_of(' '); // Read from number and return the index of the first space found.
// Use this number to split the string into firstname and lastname. Convert
// the split string into a double using the stod function.
Salary = stod(Number.substr(0, IndexOfIncrease));
Increase = stod(Number.substr(IndexOfIncrease));
// Send the converted doubles to our salarycalc function.
OutputSalary = salary_calc(Salary, Increase);
// Write the data to file.
ModifiedData << LastName << FirstName << " " << OutputSalary << endl;
}
}
double salary_calc(double CurrentSalary, double IncreaseRate) {
return CurrentSalary * (1.0 + IncreaseRate / 100.0);
}