Numdays class need troubleshoot help!!

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;

}


First of all,you dont need to include ,iostream. and <cstdlib> twice.Include it in your NumDays.h file,then include that file in your main.cpp.

I also tried running your program,but it seems to be going in a loop.

When you initialize a object of a class with a constructor,you use the '.' operator to access it.
And you use the '->' operator to access a object of a class made by using pointers.

One thing I learned in doing my self projects was that when everything seemed not to work,I started from the beginning again.But this time, I checked every function I wrote before implementing it in my program.

Maybe you should try this,but its up to you.
closed account (z05DSL3A)
You need to add operator<< and operator+= to your code.

For operator<<:
Change int getHours() { return hoursWorked; }
to int getHours() const { return hoursWorked; }
and add the following after your class definition;

1
2
3
4
5
std::ostream& operator<<(std::ostream& os, const NumDays& obj)
{
	
	return os << obj.getHours();
}


I'll leave the operator+= to you...
For your ouput to console did you mean
1
2
3
4
5
cout << "John's initial hours worked: " << hoursWorked_John.getHours() << endl;
hoursWorked_John.addHours(56.78);
cout << " John's final hours worked: " << hoursWorked_John.getHours() << endl << endl;

Topic archived. No new replies allowed.