My program consist of 3 files. The clockType.h - the class function prototypes, clockTypeImp.cpp - definitions of functions, and the testing program testClockClass.cpp.
I am trying to pass the return value of the first function to the next function in the code below. I think I am supposed to pass it as a reference perimeter, or a pointer*.
You can see my failed attempt at assigning the return value to the variable diffSec.
(What I am trying to do is convert the difference in seconds between two clocks and out put that difference as HH:MM:SS).
Part of my clockTypeImp.cpp implementation file:
int clockType::clockDiffseconds(clockType otherClock) {
int elapsedSec;
return (abs(elapsedTimeSeconds() - otherClock.elapsedTimeSeconds()));
}
void clockType::secondsToHHMMSS() {
int diffSec = clockType::clockDiffseconds(clockType otherClock);
int diffHours = diffSec/3600;
int diffMinutes = (diffSec/60)`;
int diffSeconds = diffSec`;
cout << "Converted to HH:MM:SS: " << diffHours << ":" << diffMinutes << ":" << diffSeconds;
}
Part of my clockType.h file where clockType is coming from:
class clockType {
public:
void remainingTimeSeconds(int& totalEndSec); // Function to convert clock to time remaining in seconds.
int clockDiffseconds(clockType otherClock); // Function for finding difference in time between clocks.
void secondsToHHMMSS(); // Function for converting seconds to HH:MM:SS.
bool equalTime(const clockType& otherClock) const; // Function to compair the two times.
private:
int hr; // Variable to store the hours.
int min; // Variable to store the minutes.
int sec; // Variable to store the seconds.
};
Here is part of my testing program.
#include <iostream>
#include <iomanip>
#include "clockType.h"
using namespace std;
int main() {
clockType myClock;
clockType yourClock;
int hours;
int minutes;
int seconds;
int elapsedSec;
int totalEndSec;
cout << endl << "myClock Time is not set:\n";
myClock.printTime();
cout << endl << "myClock Time is set:\n";
myClock.setTime(9, 3, 30);
myClock.printTime();
cout << endl;
cout << endl << "yourClock Time is not set:\n";
yourClock.printTime();
cout << endl << "yourClock Time is set:\n";
yourClock.setTime(5, 45, 16);
yourClock.printTime();
cout << endl << endl;
if (myClock.equalTime(yourClock)) {
cout << "Both times are equal." << endl;
} else {
cout << "The two times are not equal." << endl;
}
cout << endl << "Set the time for myClock\nEnter the hours, minutes, and seconds:\n";
yourClock.remainingTimeSeconds(totalEndSec);
cout << "\x1B[32m-->\033[0m" << " The total remaining seconds of \x1B[32myourClock\033[0m is: " << totalEndSec << endl;
myClock.remainingTimeSeconds(totalEndSec);
cout << "\x1B[33m-->\033[0m" << " The total remaining seconds of \x1B[33mmyClock\033[0m is: " << totalEndSec << endl;
cout << endl;
cout << "\x1B[34m-->\033[0m" << " The difference in seconds between the \x1B[34mtwo clocks\033[0m is: " << myClock.clockDiffseconds(yourClock) << endl;
Let me know if you need to see the full code of the file(s).
CodePudding user response:
clockDiffseconds() is a non-static method that acts on this, calculating the difference in seconds between this and another clock otherClock. That is fine.
But secondsToHHMMSS() has no concept of otherClock. It is also a non-static method that acts only on this. So, for secondsToHHMMSS() to be meaningful, you have a few choices:
re-think what
secondsToHHMMSS()actually means to you. The way you have written it, it should just print the current seconds ofthisas-is. In which case, use the value returned byclockDiffseconds()to constructs a newclockTypeobject whoseelapsedTimeSeconds()returns just that value, and then callsecondsToHHMMSS()on that newclockTypeobject, eg:class clockType { public: ... clockType clockDiff(clockType otherClock) const; void secondsToHHMMSS() const; ... };clockType clockType::clockDiff(clockType otherClock) const { int elaspedSec = abs(elapsedTimeSeconds() - otherClock.elapsedTimeSeconds()); return clockType(elaspedSec); // add a constructor to clockType that stores elaspedSec in such // a way that its elapsedTimeSeconds() can then return it... } void clockType::secondsToHHMMSS() const { int elapsedSec = elapsedTimeSeconds(); int hours = elapsedSec/3600; int minutes = (elapsedSec/60)`; int seconds = elapsedSec`; cout << "Converted to HH:MM:SS: " << hours << ":" << minutes << ":" << seconds; }clockType myClock, yourClock, elapsed; ... elapsed = myClock.clockDiff(yourClock); elapsed.secondsToHHMMSS(); ...Otherwise, if you really want
secondsToHHMMSS()to express the difference in seconds between two clocks, then you will have to pass in theotherClockas a parameter tosecondsToHHMMSS(), just like you already do withclockDiffseconds(), eg:class clockType { public: ... int clockDiffseconds(clockType otherClock) const; void secondsToHHMMSS(clockType otherClock) const; ... };int clockType::clockDiffseconds(clockType otherClock) const { return abs(elapsedTimeSeconds() - otherClock.elapsedTimeSeconds()); } void clockType::secondsToHHMMSS(clockType otherClock) const { int diffSec = clockDiffseconds(otherClock); int diffHours = diffSec/3600; int diffMinutes = (diffSec/60)`; int diffSeconds = diffSec`; cout << "Converted to HH:MM:SS: " << diffHours << ":" << diffMinutes << ":" << diffSeconds; }clockType myClock, yourClock; ... myClock.secondsToHHMMSS(yourClock); ...Otherwise, consider changing
secondsToHHMMSS()to be astaticmethod instead, and then pass in the desired seconds as a parameter, such as from an earlier call toclockDiffseconds(), eg:class clockType { public: ... static void secondsToHHMMSS(int seconds); ... };void clockType::secondsToHHMMSS(int seconds) const { int hours = seconds/3600; int minutes = (seconds/60)`; seconds = seconds`; cout << "Converted to HH:MM:SS: " << hours << ":" << minutes << ":" << seconds; }clockType myClock, yourClock; ... int diffSec = myClock.clockDiffseconds(yourClock); clockType::secondsToHHMMSS(diffSec); ...
