C ++十进制到时间转换问题

时间:2019-03-28 23:46:33

标签: c++

我的作业要求我将时间转换为分钟,但是必须像HH.MM一样插入时间,中间要带小数点。因此,上午9:25将作为9.25输入到程序中。但是,问题在于9.25不等于9.25am,而是等于9:15 am。我有第二个变量,它将9.25转换为整数形式,从9.25中删除了.25。

我曾尝试通过乘以9.25 * 60将9:25转换为分钟,但是由于9.25不等于上午9:25,因此最终将导致错误的答案。

    cout << "Enter the starting hour of the call 
    cin >> startTime;

    cout << "Enter the total number of minutes for this call: ";
    cin >> minutes;


    int getwin = startTime; 
    int startHH = getwin / 60; 
    int startMM = getwin % 60;

我应该能够输出一个电话的开始时间,为该电话增加分钟,并输出该电话的结束时间。因此,例如: 开始时间:9.25 通话时间:180分钟 结束时间:12.25

1 个答案:

答案 0 :(得分:1)

您的问题是您将注意力集中在“十进制”点上。它不是小数点,而是定界符。 您可以输入整个字符串并分别解析小时和分钟(直观的方式),也可以使用cin输入浮点数,例如下面的代码:

   double input;
   cin >>input;
   int h = (int) input; // the integer part is hours
   double mm = input - h; //the non-integer part is minutes
   mm*=100;// the minutes are less than one ,we have to multiply by 100
   int m = (int) m;

   cout <<h<<":"<<m<<endl;
相关问题