employee.h
----------
#include <stdio.h>
#include <iostream>
#include <fstream>
class Employee {
private:
int employeeNum;
std::string name;
std::string address;
std::string phoneNum;
double hrWage, hrWorked;
public:
Employee(int en, std::string n, std::string a, std::string pn,
double hw, double hwo);
std::string getName();
void setName(std::string n);
int getENum();
std::string getAdd();
void setAdd(std::string a);
std::string getPhone();
void setPhone(std::string p);
double getWage();
void setWage(double w);
double getHours();
void setHours(double h);
double calcPay(double a, double b);
static Employee read(std::ifstream& in);
void write(std::ofstream& out);
};
employee.cpp
----------
//employee.cpp
#include "employee.h"
#include <iostream>
#include <string>
using namespace std;
Employee::Employee(int en, std::string n, std::string a,
std::string pn, double hw, double hwo) {
this->employeeNum = en;
this->name = n;
this->address = a;
this->phoneNum = pn;
this->hrWage = hw;
this->hrWorked = hwo;
}
string Employee::getName() {
return name;
}
void Employee::setName(string n) {
this->name = n;
}
int Employee::getENum() {
return employeeNum;
}
string Employee::getAdd() {
return address;
}
void Employee::setAdd(string a) {
this->address = a;
}
string Employee::getPhone() {
return phoneNum;
}
void Employee::setPhone(string p) {
this->phoneNum = p;
}
double Employee::getWage() {
return hrWage;
}
void Employee::setWage(double w) {
this->hrWage = w;
}
double Employee::getHours() {
return hrWorked;
}
void Employee::setHours(double h) {
this->hrWorked = h;
}
double Employee::calcPay(double a, double b) {
const double x = 40.00;
double gross;
if(b > 40)
{
b -= x;
gross = (a * x) + ((a + (a / 2)) * b);
}
else {
gross = a * b;
}
double net = gross - (gross * .2) - (gross * .075);
return net;
}
Employee Employee::read(ifstream &in){
string name, addr, phone;
double wage, hours;
int id;
in >> id;
if(in.fail())
throw runtime_error("end of file");
in.ignore(); //get rid of newline before using getline()
getline(in, name);
getline(in, addr);
getline(in, phone);
in >> wage >> hours;
if(in.fail())
throw runtime_error("end of file");
return Employee(id, name, addr, phone, wage, hours);
}
void Employee::write(ofstream &out)
{
out << employeeNum << endl;
out << name << endl;
out << address << endl;
out << phoneNum << endl;
out << hrWage << endl;
out << hrWorked << endl;
}
main.cpp
========
#include "employee.h"
#include <iostream>
#include <iomanip>
using namespace std;
void PrintCalc(Employee a) {
cout << "Employee Name: "<< a.getName() <<
endl;
cout << "Employee Number: " << a.getENum() <<
endl;
cout << "Address: " << a.getAdd() << endl;
cout << "Phone: " << a.getPhone() << endl;
cout << "....................UVU COMPUTER SCIENCE
Dept.................................\n\n";
cout << "Pay to the order of " << a.getName() <<
".................................... $" << fixed <<
setprecision(2) << a.calcPay(a.getWage(), a.getHours())
<< endl << endl;
cout << "United Community Credit Union\n";
cout <<
"..............................................................................\n";
cout << "Hours worked: " << fixed <<
setprecision(2) << a.getHours() << endl;
cout << "Hourly wage: " << a.getWage() << endl
<< endl;
}
void createFile()
{
string filename;
Employee joe(37, "Joe Brown", "123 Main St.", "123-6788", 45.00,
10.00);
Employee sam(21, "Sam Jones", "45 East State", "661-9000", 30.00,
12.00);
Employee mary(15, "Mary Smith", "12 High Street", "401-8900",
40.00, 15.00);
cout << "Please enter a file name: ";
cin >> filename;
ofstream out(filename.c_str());
joe.write(out);
sam.write(out);
mary.write(out);
out.close();
cout << "Data file created ... you can now run option 2."
<< endl;
}
void readFile()
{
string filename;
cout << "Please enter a file name: ";
cin >> filename;
ifstream in(filename.c_str());
if(in.fail())
throw runtime_error("Couldn't open file for input");
else
{
for(int i = 0; i < 3; i++)
{
Employee e = Employee::read(in);
PrintCalc(e);
}
in.close();
}
}
int main(int argc, const char * argv[]) {
int choice = 0;
cout << "This program has two options: " <<
endl;
cout << "1 - Create a data file, or" << endl;
cout << "2 - Read data from a file and print paychecks."
<< endl;
cout << "Please enter (1) to create a file or (2) to print
checks: " ;
cin >> choice;
try {
if(choice == 1)
createFile();
else if(choice == 2)
readFile();
else
cout << "Invalid choice!" << endl;
} catch (runtime_error e) {
cout << e.what() << endl;
}
return 0;
}
It keep showing these errors and I need help correcting the code above
Hello,
The program doesn't have any error that you specified. Only
error which I got was as follows in the main.cpp
error C2679: binary '<<' : no operator found which takes a
right-hand operand of type 'std::string' (or there is no acceptable
conversion)
The error is corrected by including the header file #include
<string> in main.cpp and I was able to execute and see the
output given in screenshots.
Apart from these, there are not error found. But anyway I have identified the cause for the error based on your screenshots. If you still facing the issue, implement the below solutions:
Constructor and Getters
emp is instance of Class Employee and this class doesn't have methods getEmployeeNumber(), getAddr(), getHourlyWage() and getHoursWorked()
Solution: Replace the following methods with below given methods:
Setters
emp is instance of Class Employee and this class doesn't have methods setAddr(), setHourlyWage() and setHoursWorked()
Solution: Replace the following methods with below given methods
calcPay
read() and write()
emp is instance of Class Employee and this class doesn't have methods getEmployeeNumber(), getAddr(), getHourlyWage(), getHoursWorked()
Solution: Replace the following methods with below given methods
Feel free to ask further doubts. Always here to clarify
employee.h ---------- #include <stdio.h> #include <iostream> #include <fstream> class Employee { private: int employeeNum; std::string name;...
Answer this in c++ #include <iostream> #include <fstream> #include <string> using namespace std; class Person { public: Person() { setData("unknown-first", "unknown-last"); } Person(string first, string last) { setData(first, last); } void setData(string first, string last) { firstName = first; lastName = last; } void printData() const { cout << "\nName: " << firstName << " " << lastName << endl; } private: string firstName; string lastName; }; class Musician : public Person { public: Musician() { // TODO: set this...
//Vehicle.h #pragma once #include<iostream> #include"Warranty.h" #include<string> using namespace std; class Vehicle{ protected: string make; int year; double mpg; Warranty warranty; static int numOfVehicles; public: Vehicle(); Vehicle(string s, int y, double m, Warranty warranty); Vehicle(Vehicle& v); ~Vehicle(); string getMake(); int getYear(); double getGasMileage(); void setMake(string s); void setYear(int y); void setYear(string y); void setGasMileage(double m); void setGasMileage(string m); void displayVehicle(); static int getNumVehicles(); Warranty getWarranty(); void setWarranty(Warranty& ); }; //Vehicle.cpp #include "Vehicle.h" #include <string> Vehicle::Vehicle() { make = "unknown"; year =...
Need to implement Account.cpp and AccountManager.cpp code //Account.hpp #ifndef _ACCOUNT_HPP_ #define _ACCOUNT_HPP_ #include <string> using std::string; class Account { public: Account(); Account(string, double); void deposit(double); bool withdraw(double); string getName() const; double getBalance() const; private: string name; double balance; }; #endif ////////////////////////////////////////////// //AccountManager.hpp #ifndef _ACCOUNT_MANAGER_HPP_ #define _ACCOUNT_MANAGER_HPP_ #include "Account.hpp" #include <string> using std::string; class AccountManager { public: AccountManager(); AccountManager(const AccountManager&); //copy constructor void open(string); void close(string); void depositByName(string,double); bool withdrawByName(string,double); double getBalanceByName(string); Account getAccountByName(string); void openSuperVipAccount(Account&); void closeSuperVipAccount(); bool getBalanceOfSuperVipAccount(double&) const;...
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...
#include <iostream> #include <vector> #include <fstream> #include <time.h> #include <chrono> #include <sstream> #include <algorithm> class Clock { private: std::chrono::high_resolution_clock::time_point start; public: void Reset() { start = std::chrono::high_resolution_clock::now(); } double CurrentTime() { auto end = std::chrono::high_resolution_clock::now(); double elapsed_us = std::chrono::duration std::micro>(end - start).count(); return elapsed_us; } }; class books{ private: std::string type; int ISBN; public: void setIsbn(int x) { ISBN = x; } void setType(std::string y) { type = y; } int putIsbn() { return ISBN; } std::string putType() { return...
Convert to use functions where possible #include<iostream> #include<string> using namespace std; int main() { string first, last, job; double hours, wages, net, gross, tax, taxrate = .40; double oPay, oHours; int deductions; // input section cout << "Enter First Name: "; cin >> first; cout << "Enter Last Name: "; cin >> last; cin.ignore(); cout << "Enter Job Title: "; getline(cin, job); cout << "Enter Hours Worked:...
please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...
#include <iostream> using namespace std; class Circle{ // private member variable named radius private: double radius; // get function for radius public: double getRadius(){ return radius; } // set function for radius void setRadius(double rad){ radius=rad; } // returning area = 3.14159 * radius * radius double getArea(){ return (3.14159 * radius * radius); } }; // Sample run int main() { // Declaring object of Circle Circle myCircle; myCircle.setRadius(5); // printing radius of circle cout<<"Radius of circle is: "<<(myCircle.getRadius())<<endl;...
#include <iostream> #include <string> using std::string; using std::cout; using std::endl; void testAnswer(string testname, int answer, int expected) { if (answer == expected) cout << "PASSED: " << testname << " expected and returned " << answer << "\n"; else cout << "FAILED: " << testname << " returned " << answer << " but expected " << expected << "\n"; } // Implement printArray here void printArray(int array[],int b) { for(int i = 0; i<b;i++) { std::cout<< array[i] << "...
CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...