I'm in a beginner level C++ class and I'm having some trouble figuring out why I'm getting the wrong output when I convert minutes into days, hours, minutes and seconds in C++.
I had a friend help me and she was able to get the correct output in Python using the same math, but for some reason in C++ I can't get the correct output for the seconds. I'm sure there's a much much easier way to get the output, but this is how to professor wants us to write it.
For example, the input would be 10.5 and the code outputs 0d 0h 10m 0s instead of 0d 0h 10m 30s.
Here's the code:
#include <iostream>
using namespace std;
int main() {
//get the starting number of minutes
int inputMinutes;
double decimalMinutes;
cout << "Enter number of minutes: ";
cin >> decimalMinutes;
//allow decimal input
inputMinutes = decimalMinutes;
//get number of days
int days = inputMinutes / 1440;
inputMinutes = inputMinutes % 1440;
//get number of hours
int hours = inputMinutes / 60;
//get number of minutes
int mins = inputMinutes % 60;
int seconds = inputMinutes % 1 * 60;
//output days, hours, minutes and seconds
cout << days << "d " << hours << "h " << mins << "m " << seconds << "s" << endl;
}
I have a feeling it's something to do with casting the int to a double but if that's not the case, then I'm not sure what could be wrong with it. Any help is appreciated, thanks.