fork download
  1. /*
  2. Salary Increase Calculator
  3. Name: Reagan Fischer
  4. CSC160.1N2
  5. This program reads names, salaries, and proposed increases in pay from a hard-coded data file called Ch3_Ex6Data.txt.
  6. It then writes calculated salary increases to Ch3_Ex6Output.dat.
  7. */
  8.  
  9. #include <fstream>
  10. #include <iomanip>
  11. #include <iostream>
  12. #include <string>
  13.  
  14. using namespace std;
  15. double salary_calc(double CurrentSalary, double IncreaseRate); // Declare function for compiler to avoid errors.
  16.  
  17. int main() {
  18. ifstream SalaryData; // Define the filestream object that will contain our data.
  19. ofstream ModifiedData; // Define the filestream object that we will output to.
  20. // Define strings to hold the entire current line, current name, the split name, and the
  21. // current salary/increase.
  22. string SalaryData_Line;
  23. string Name;
  24. string FirstName;
  25. string LastName;
  26. string Number;
  27. // Define doubles to hold the current salary, the increase, and the final
  28. // salary.
  29. double Salary = 0.0;
  30. double Increase = 0.0;
  31. double OutputSalary = 0.0;
  32. // Define size_t variables to hold the index of the char in our string where
  33. // the salary data starts and where the salary increase data starts.
  34. size_t IndexOfSalary = 0;
  35. size_t IndexOfIncrease = 0;
  36. size_t IndexOfName = 0;
  37. // Open our files for reading and writing.
  38. SalaryData.open("Ch3_Ex6Data.txt");
  39. ModifiedData.open("Ch3_Ex6Output.dat");
  40. // Set our output file to write floating point numbers with the decimal always
  41. // showing and with a fixed precision of two.
  42. ModifiedData << fixed << showpoint << setprecision(2);
  43. // Begin reading from our file handle in a loop and saving , terminating when
  44. // EOL is reached.
  45. while (getline(SalaryData, SalaryData_Line)) {
  46. IndexOfSalary = SalaryData_Line.find_first_of("0123456789"); // Read from salarydata_line and return the index of the first number found.
  47. // Use this number to split the string into name and number.
  48. Name = SalaryData_Line.substr(0, IndexOfSalary);
  49. Number = SalaryData_Line.substr(IndexOfSalary);
  50. IndexOfName = SalaryData_Line.find_first_of(' '); // Read from name and return the index of the first space found.
  51. // Use this number to split the string into firstname and lastname.
  52. FirstName = Name.substr(0, IndexOfName);
  53. LastName = Name.substr(IndexOfName);
  54. IndexOfIncrease = Number.find_first_of(' '); // Read from number and return the index of the first space found.
  55. // Use this number to split the string into firstname and lastname. Convert
  56. // the split string into a double using the stod function.
  57. Salary = stod(Number.substr(0, IndexOfIncrease));
  58. Increase = stod(Number.substr(IndexOfIncrease));
  59. // Send the converted doubles to our salarycalc function.
  60. OutputSalary = salary_calc(Salary, Increase);
  61. // Write the data to file.
  62. ModifiedData << LastName << FirstName << " " << OutputSalary << endl;
  63. }
  64. }
  65.  
  66. double salary_calc(double CurrentSalary, double IncreaseRate) {
  67. return CurrentSalary * (1.0 + IncreaseRate / 100.0);
  68. }
Success #stdin #stdout 0.01s 5448KB
stdin
Standard input is empty
stdout
Standard output is empty