why is elapsedtime giving me an output of 1? [clos

2019-09-22 06:22发布

int elapsedtime(int time1, int time2)
{
int minutes1 = ((time1/100*60)+(time1%100);
int minutes2 = ((time2/100*60)+(time2%100);
return(minutes2-minutes1);
}

/**************************************************************************************

do
    {
        cout << "What is your starting time? ";
        cin >> time1;
        cout << "What is your ending time? ";
        cin >> time2;

        if (time2>=time1)
        {
        cout << "Your elapsed time is " << elapsedtime <<endl;
        cout << "If you would like to enter other times?(Y/N)" <<endl;
        cin >> choice;
        }
        else
        {
        cout << "Error: Your starting time must be lower than your ending time."<<endl;
        cout << "If you would like to enter other times?(Y/N)" <<endl;
        cin >> choice;
        }
    }

I keep getting an output of 1 for every elapsed time?

1条回答
做自己的国王
2楼-- · 2019-09-22 06:27

You have

    cout << "Your elapsed time is " << elapsedtime <<endl;

Did you mean

    cout << "Your elapsed time is " << elapsedtime(time1, time2) <<endl;

Update

For some reason,

    cout << "Your elapsed time is " << elapsedtime <<endl;

does not produce a compiler error under g++ 4.7.3 even though the intent is to use elapsedtime(time1, time2).

I was able to successfully compile and build:

#include <iostream>
using namespace std;

int elapsedtime(int time1, int time2)
{
   return 0;
}

int main()
{
   cout << "Your elapsed time is " << elapsedtime <<endl;
}

Perhaps another SO post is needed to answer the question, "How does g++ not report error with the above code?".

查看更多
登录 后发表回答