Question
c++ visual studio 2019 Do not use #include Help me improve my program: was told that not all possible errors were considered ( all possible
c++ visual studio 2019
Do not use #include
Help me improve my program: was told that not all possible errors were considered (all possible errors: wrong data file name, wrong record size put in)
original question:
Change the database.cpp program to throw exceptions when there is an unexpected problem. When appropriate offer the user an option to fix the problem. (all possible errors that can occur in this program)
ccc_empl.h-----------------------------------------------
#ifndef CCC_EMPL_H #define CCC_EMPL_H
#include
using namespace std;
/** A basic employee class that is used in many examples in the book "Computing Concepts with C++ Essentials" */ class Employee { public: /** Constructs an employee with empty name and no salary. */ Employee(); /** Constructs an employee with a given name and salary. @param employee_name the employee name @param initial_salary the initial salary */ Employee(string employee_name, double initial_salary); /** Sets the salary of this employee. @param new_salary the new salary value */ void set_salary(double new_salary); /** Gets the salary of this employee. @return the current salary */ double get_salary() const; /** Gets the name of this employee. @return the employee name */ string get_name() const; private: string name; double salary; };
#endif
ccc_empl.cpp--------------------------------------
#include "ccc_empl.h"
Employee::Employee() { salary = 0; }
Employee::Employee(string employee_name, double initial_salary) { name = employee_name; salary = initial_salary; }
void Employee::set_salary(double new_salary) { salary = new_salary; }
double Employee::get_salary() const { return salary; }
string Employee::get_name() const { return name; }
database.cpp------------------------------------------------
#include
using namespace std;
#include "ccc_empl.h"
const int NEWLINE_LENGTH = 2; const int RECORD_SIZE = 30 + 10 + NEWLINE_LENGTH;
int main(); Employee e;
/** Converts a string to a floating-point value, e.g. "3.14" -> 3.14. @param s a string representing a floating-point value @return the equivalent floating-point value */ double string_to_double(string s) { istringstream instr(s); double x; instr >> x; return x; }
/** Raises an employee salary. @param e employee receiving raise @param percent the percentage of the raise */ void raise_salary(Employee& e, double percent) { double new_salary = e.get_salary() * (1 + percent / 100); e.set_salary(new_salary); }
/** Reads an employee record from a file. @param e filled with the employee @param in the stream to read from */
void read_employee(Employee& e, istream& in) { try { string line; getline(in, line); if (in.fail() || in.bad()) throw 0; else { string name = line.substr(0, 30); double salary = string_to_double(line.substr(30, 10)); e = Employee(name, salary); } } catch (...) { cout << " Error reading file." << endl; main(); } }
/** Writes an employee record to a stream. @param e the employee record to write @param out the stream to write to */ void write_employee(Employee e, ostream& out) { out << e.get_name() << setw(10 + (30 - e.get_name().length())) << fixed << setprecision(2) << e.get_salary(); }
int main() { cout << "Please enter the data file name: "; string filename; cin >> filename; fstream fs; fs.open(filename.c_str()); read_employee(e, fs); fs.seekg(0, ios::end); // Go to end of file int nrecord = fs.tellg() / RECORD_SIZE;
cout << "Please enter the record to update: (0 - " << nrecord - 1 << "): "; int pos; cin >> pos; const double SALARY_CHANGE = 5.0; fs.seekg(pos * RECORD_SIZE, ios::beg); raise_salary(e, SALARY_CHANGE); cout << "New salary: " << e.get_salary() << endl; fs.seekp(pos * RECORD_SIZE, ios::beg); write_employee(e, fs); fs.close(); system("pause"); return 0; }
employee.dat--------------------------------------------------------
Johnson, Marianne 36118.95 Sanchez, Linda 31695.00 Lee, John 32798.90 Baldassarian, George S. 25895.00 Karamanlaki, Alexander 27508.95 Schmidt, Otto 29316.00 Stroustrup, Bjarne 124495.00 Keller, Chris 23318.04 Carter 3rd, Hodding 26632.00
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started