mardi 26 avril 2016

Incorrect output from overloaded << operator c++

I have Class NumDays as shown:

Class NumDays
{
   private:
      double hours;

   public:
      NumDays() { hours = 0.0; }            //default constructor
      NumDays(double hr) { hr = hours; }    //initializing constructor  

   //Large class, nothing of importance, rest of class omitted

   //overloading << operator
    friend ostream &operator<<(ostream &out, NumDays a);
}

I have NumDay.cpp that includes:

ostream &operator<<(ostream& out, NumDays a)
{
   // takes amount of hours, computes to work days

   int temp = a.hours / 8;

   //gives remainder of hours after full 8 hr workday.

   double hrs = a.hours - (temp * 8);

   //outputs 
   cout << fixed << setprecision(0);
   out << (a.hours / 8) << " Days, " << hrs << "hours";
    return out;
}

And I have main.cpp to include:

int main()
{
   // Initialized UDT object Declarations
   NumDays hoursWorked_John;       // Instantiate with Default Constructor
   NumDays hoursWorked_Sue(36.9);  // Instantiate with Initializing Cons      
   NumDays hoursUsed_Sue(4.5);     // Instantiate with Initializing Cons

   cout << "John's initial hours worked: " << hoursWorked_John << endl;
   hoursWorked_John.addHours(56.78);
   cout << "  John's final hours worked: " << hoursWorked_John << endl;

   cout << "Sue's initial hours worked: " << hoursWorked_Sue << endl;

   //rest of main omitted for sake of size

When I go to run this small section of the program, this is my console:

Consle Output

Any thoughts on why Sue's hours are so fantastically wrong, but John's are correct?

Aucun commentaire:

Enregistrer un commentaire