Help with incorrect output when Adding and Subtracting Time

The program currently adds and subtracts time correctly by not allowing the time to go into a negative number or into military time and beyond. The problem is when say 2:00 and 3:00 is added together, the program defaults to 12:00. When the minutes equal 0 it defaults. I seem to keep overlooking the obvious and need some outside eyes, some outside opinions. Thank you for your time.

.......Header file......
#ifndef TIME_H
#define TIME_H

class Time
{

private:
int hours, minutes;
void simplify();
public:
Time(int h = 0, int m = 0)
{
hours = h, minutes = m, simplify();
}

void setData(int h, int m)
{
hours = h; minutes = m; simplify();
}
int getHours()
{
while (minutes <= 0 || minutes > 720)
{
if (minutes <= 0)
{
minutes = minutes + 720;
}
else if (minutes > 720)
{
minutes = minutes - 720;
}
hours = minutes / 60;
}

return hours;
}
int getMinutes()
{
while (minutes <= 0 || minutes > 720)
{
if (minutes <= 0)
{
minutes = minutes + 720;
}
else if (minutes > 720)
{
minutes = minutes - 720;
}
}
minutes = minutes % 60;

return minutes;
}
Time operator + (const Time &) const;
Time operator - (const Time &) const;
}
#endif

...........Math cpp file..............

#include "Time.h"

void Time::simplify()
{

minutes = 60 * hours + minutes;
hours = minutes / 60;
minutes = minutes % 60;
}

//**********************************************
// Overloaded binary + operator. *
//**********************************************
Time
Time::operator+(const Time &right) const
{
Time temp;
temp.minutes = minutes + right.minutes;
temp.hours = hours + right.hours;
temp.simplify();
while (temp.hours > 12)
{
temp.hours = temp.hours - 12;

if (temp.minutes >= 60) {
temp.minutes -= 60;
temp.hours++;
}
}
return temp;
}

//**********************************************
// Overloaded binary - operator. *
//**********************************************
Time
Time::operator-(const Time &right) const
{
Time temp;
temp.minutes = right.minutes - minutes;
temp.hours = right.hours - hours;
temp.simplify();

if (temp.minutes < 0)
{ temp.minutes = temp.minutes - 60; temp.hours = temp.hours - 1; }
if (temp.hours < 1)
{ temp.hours = temp.hours + 12; }
if (temp.hours > 12)
{ temp.hours = temp.hours - 12; }

if (temp.minutes >= 60) {
temp.minutes += 60;
temp.hours++;

} return temp;
}

............MAIN..............

#include <iostream>
#include "Time.h"
#include <Windows.h>
using namespace std;

int main()

{
Time first, second, third, fourth, fifth;
int h, m;
cout << "Enter Hours and Minutes ";
cin >> h >> m;

while (h > 12 || m >60)
{
if (h > 12 || m > 60)
{
cout << "\t\t\t!!! Invalid time !!! \n\n";
cout << "\tRe-enter time: ";
cin >> h >> m;
cout << "\n";
}
}

first.setData(h, m);
cout << "Enter Hours and Minutes to be added ";
cin >> h >> m;

while (h > 12 || m >60)
{
if (h > 12 || m > 60)
{
cout << "\t\t\t!!! Invalid time !!! \n\n";
cout << "\tRe-enter time: ";
cin >> h >> m;
cout << "\n";
}
}
second.setData(h, m);

third = first + second;

if (third.getMinutes() < 10)
{
cout << "\tNew time: " << third.getHours() << ":0" << third.getMinutes() << "\n\n";
}
else
{
cout << "\tNew time: " << third.getHours() << ":" << third.getMinutes() << "\n\n";
}

cout << "Enter Hours and minutes to be subtracted ";
cin >> h >> m;

while (h > 12 || m >60)
{
if (h > 12 || m > 60)
{
cout << "\t\t\t!!! Invalid time !!! \n\n";
cout << "\tRe-enter time: ";
cin >> h >> m;
cout << "\n";
}
}

fourth.setData(h, m);

fifth = fourth - third;

if (fifth.getMinutes() < 10)
{
cout << "\tNew time: " << fifth.getHours() << ":0" << fifth.getMinutes() << "\n\n";
}
else
{
cout << "\tNew time: " << fifth.getHours() << ":" << fifth.getMinutes() << "\n\n";
}

system("pause");
return 0;
}
Anybody?
People are significantly more likely to help when you put your code in [code] tags.

You don't need <windows.h> in main.cpp.

Don't name your .cpp and .hpp files different things. Your files should be named "time.hpp", "time.cpp", and "main.cpp".

(Avoid naming your sources the same as standard files.)

The problem is due to your get accessors. I don't know why you have all that logic in there checking for <= 0 or where you got the number 720 from. Get rid of it all.

1
2
int getHours() const { return hours; }
int getMinutes() const { return minutes; }

Pay attention to your PREREQUISITES. Whenever ANY function finishes which modifies hours or minutes, you MUST call simplify(). This is the function that should make sure nothing is negative or out of range. (Experienced programmers would call it "normalize".) That way the hours and minutes are always correct when needed.

You should try to simplify the normalization function. Hint: normalize the minutes first, and then the hours.

Hope this helps.

[edit] Oh, I forgot to mention, you should have I/O stream operators (or at least some functions) to read and write your times.
Last edited on
Topic archived. No new replies allowed.