Apr 27, 2015 at 12:30am UTC
I cannot seem to get this to run, I'm self learning, so this isn't as homework assignment. My errors are on the << and -=hours worked. Any advice or pointers in the right direction would help, I feel like I am right on the edge.
Here is my Numdays.h file:
ifndef NUMDAYS_H
#define NUMDAYS_H
#include <cstdlib>
#include <iostream>
using namespace std;
class NumDays
{
private:
int hoursWorked;
public:
NumDays(int hW)
{
addHours(hW);
}
NumDays(){ addHours(0); }
// Getters
double getDays(){ return hoursWorked / 8.0; }
int getHours() { return hoursWorked; }
// Setters
void addHours(int hW){ hoursWorked = hW; }
void setDays(double d) { addHours(8 * d); }
friend NumDays operator + (NumDays, NumDays);
friend NumDays operator - (NumDays, NumDays);
// Prefix operators
NumDays operator++()
{
++hoursWorked;
return *this;
}
NumDays operator--()
{
--hoursWorked;
return *this;
}
// Postfix operators
NumDays operator++(int)
{
NumDays temp = *this;
hoursWorked++;
return temp;
}
NumDays operator--(int)
{
NumDays temp = *this;
hoursWorked--;
return temp;
}
};
/***********************************************
* operator + *
***********************************************/
NumDays operator+(NumDays a, NumDays b)
{
return NumDays(a.hoursWorked + b.hoursWorked);
}
/***********************************************
* operator - *
***********************************************/
NumDays operator-(NumDays a, NumDays b)
{
return NumDays(a.hoursWorked - b.hoursWorked);
}
#endif
*******************************************************************************
Here is the .cpp
#include "stdafx.h"
#include <iostream>
#include "NumDays.h"
#include <cstdlib>
using namespace std;
int main()
{
// Initialized UDT object Declarations
NumDays hoursWorked_John (8); // Instantiate with Default Constructor
NumDays hoursWorked_Sue(36.9); // Instantiate with Initializing Constructor
NumDays hoursUsed_Sue(4.5); // Instantiate with Initializing Constructor
cout << "John's initial hours worked: " << hoursWorked_John << endl;
hoursWorked_John.addHours(56.78);
cout << " John's final hours worked: " << hoursWorked_John << endl << endl;
cout << "Sue's hours worked: " << hoursWorked_Sue << endl;
cout << "Total hours worked: " << (hoursWorked_John + hoursWorked_Sue)
<< endl << endl;
hoursWorked_Sue -= hoursUsed_Sue;
cout << " Sue's hours Used: " << hoursUsed_Sue << endl;
cout << "Sue's adjusted hours: " << hoursWorked_Sue << endl;
cout << "Adjusted Total hours: " << (hoursWorked_John + hoursWorked_Sue)
<< endl << endl;
cout << "Adding 39.5 hours to Sue's Used hours" << endl;
hoursUsed_Sue.addHours(39.5);
hoursWorked_Sue -= hoursUsed_Sue;
cout << " Sue's hours Used: " << hoursUsed_Sue << endl;
cout << "Sue's adjusted hours: " << hoursWorked_Sue << endl;
cout << "Adjusted Total hours: " << (hoursWorked_John + hoursWorked_Sue)
<< endl << endl;
cout << "Adding 39.5 hours to Sue's Hours Worked" << endl;
hoursWorked_Sue.addHours(39.5);
cout << " Sue's adjusted Hours worked: " << hoursWorked_Sue << endl;
cout << "Prefix increment Sue's hours worked in the display statement:" << endl;
cout << " Sue's hours worked: " << ++hoursWorked_Sue << endl;
cout << " Sue's Final hours: " << hoursWorked_Sue << endl << endl;
cout << "Postfix increment Sue's hours worked in the display statement:" << endl;
cout << " Sue's hours worked: " << hoursWorked_Sue++ << endl;
cout << " Sue's Final hours: " << hoursWorked_Sue << endl << endl;
cout << "Adding 5.67 hours to John's hours worked" << endl;
cout << " John's current hours worked: " << hoursWorked_John << endl;
hoursWorked_John.addHours(5.67);
cout << " John's adjusted Hours worked: " << hoursWorked_John << endl;
cout << "Prefix decrement John's hours worked in the display statement:" << endl;
cout << " John's Final hours: " << hoursWorked_John << endl;
cout << " John's hours worked: " << --hoursWorked_John << endl;
cout << "Postfix decrement John's hours worked in the display statement:" << endl;
cout << " John's hours worked: " << hoursWorked_John-- << endl;
cout << " John's Final hours: " << hoursWorked_John << endl;
// This prevents the Console Window from closing during debug mode
// Note: Generally, you should not remove this code
cin.ignore(cin.rdbuf()->in_avail());
cout << "\nPress only the 'Enter' key to exit program: ";
cin.get();
return 0;
}
Last edited on Apr 27, 2015 at 12:44am UTC
Apr 27, 2015 at 8:46am UTC
What error did your compiler give you? Or does it compile correctly, then crash while it's running?