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

Recommended Answers

All 5 Replies

Okay, what have you tried? We're not going to do it for you, so start pulling your weight.

#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.

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
*/

Thanks dude :D

Take a look at strftime

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.