0

Hello,

i am new to c++. i wanted to convert minutes e.g. 75, to hours and minutes in hh:mm format.

any help will be greately appreciated. thanks :D

4
Contributors
5
Replies
6
Views
12 Years
Discussion Span
Last Post by winbatch
0
#include <iostream>
using namespace std;
int main()

{
  int mint;

  cout << "Enter minutes: ";
  cin >> mint;

  int hh , mm;
  hh = mint/60;
  mm = mint % 60;

  cout << hh << ":" << mm ;
}

it should display the time in hh:mm format, but when i tested it in some occasions e.g. when entering 1 it should display 00:01, it displays 0:1. and when entering the number of minutes higher than 1440 equavalent to 24hours, it should diplay an error (same when entering numbers below 0)

thanks.

0

I think you use manipulators to set a width and zero fill.

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
  int mint;

  cout << "Enter minutes: ";
  cin  >> mint;

  int hh , mm;
  hh = mint / 60;
  mm = mint % 60;

  cout << setw(2) << setfill('0') << hh << ":"
       << setw(2) << setfill('0') << mm << '\n';

  return 0;
}

/* my output
Enter minutes: 180
03:00
*/
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.