简体   繁体   中英

How do I pass a return value to another function and assign the return value to a variable within that function?

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)%60;
    int diffSeconds = diffSec%60;
    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).

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 of this as-is. In which case, use the value returned by clockDiffseconds() to constructs a new clockType object whose elapsedTimeSeconds() returns just that value, and then call secondsToHHMMSS() on that new clockType object, 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)%60; int seconds = elapsedSec%60; 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 the otherClock as a parameter to secondsToHHMMSS() , just like you already do with clockDiffseconds() , 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)%60; int diffSeconds = diffSec%60; cout << "Converted to HH:MM:SS: " << diffHours << ":" << diffMinutes << ":" << diffSeconds; }
     clockType myClock, yourClock;... myClock.secondsToHHMMSS(yourClock);...
  • Otherwise, consider changing secondsToHHMMSS() to be a static method instead, and then pass in the desired seconds as a parameter, such as from an earlier call to clockDiffseconds() , eg:

     class clockType { public:... static void secondsToHHMMSS(int seconds);... };
     void clockType::secondsToHHMMSS(int seconds) const { int hours = seconds/3600; int minutes = (seconds/60)%60; seconds = seconds%60; cout << "Converted to HH:MM:SS: " << hours << ":" << minutes << ":" << seconds; }
     clockType myClock, yourClock;... int diffSec = myClock.clockDiffseconds(yourClock); clockType::secondsToHHMMSS(diffSec);...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM