【问题标题】:why is elapsedtime giving me an output of 1? [closed]为什么 elapsedtime 给我的输出为 1? [关闭]
【发布时间】:2014-06-25 02:59:39
【问题描述】:
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;
        }
    }

每次经过的时间我都会得到 1 的输出?

【问题讨论】:

  • Integer division 也许??
  • 对于一个你正在将所有数学作为丢弃小数部分的整数进行计算,例如3/2 = 1
  • 是的,我需要这样做才能正确转换 24 小时制。

标签: c++ function time


【解决方案1】:

你有

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

你是说

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

更新

由于某种原因,

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

在 g++ 4.7.3 下不会产生编译器错误,即使意图是使用 elapsedtime(time1, time2)

我能够成功编译和构建:

#include <iostream>
using namespace std;

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

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

也许需要另一个 SO 帖子来回答这个问题,“g++ 如何不报告上述代码的错误?”。

【讨论】:

  • 为什么没有编译错误 - 或者问题是错误的 - 我不认为这是一个答案?
  • R萨胡你是我的救星!
  • @jquilly1245 只是为了第一个手段!! :-/ ...(恐慌接受)
  • 我收到了warning 。看起来函数指针被隐式转换为bool
  • @T.C.,现在在this question 进行。是的,他们这样做了,但我不确定这样做的动机是什么,因为我很确定结果总是true
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-11
  • 2013-11-24
  • 2012-08-05
  • 1970-01-01
相关资源
最近更新 更多