Where am I going wrong converting minutes to "hours and minutes"?

I am trying to write this basic program to convert minutes to hours and minutes. I can get it to work but I need the singular minute and hour when the output is one and the plural otherwise and this is messing me up. I know I can use if else statements, but I am trying to do it with the conditional ternary as that's where I am in learning C++. Can anyone see what I am obviously missing? Thanks for any help! Here is my code:

#include <iostream>
using namespace std;
int convert_to_hour (int);
int convert_to_min (int);
int main()
{
int totalmin;
cout <<"This program converts minutes to hours and minutes. \n";
cout <<"Please enter a number of minutes (an integer) to be converted: \n";
cin >>totalmin;
cout << totalmin << " minutes equals " << (convert_to_hour(totalmin)== 1?"1 hour and ": " hours and ") << (convert_to_min(totalmin)==1? "1 minute.": " minutes.") <<endl;
return 0;
}
int convert_to_hour (int totalmin)
{
int hour=(totalmin / 60) % 24;
return (hour);
}

int convert_to_min (int totalmin)
{
int min = (totalmin % 60);
return (min);

}



1
2
const int hours=convert_to_hour(totalmin), mins=convert_to_min(totalmin);
cout << totalmin << " minutes equals " << hours << " hour" << (hours==1? "" : "s") << " and " << mins << " minute" << (mins==1? "" : "s") << endl;


or
1
2
3
4
5
6
7
8
static string pluralize(int val,const string& desc)
{
  stringstream ss;
  ss << val << ' ' << desc << (val==1? "" : "s");
  return ss.str();
}
[...]
cout << totalmin << " minutes equals " << pluralize(hours,"hour") << " and " << pluralize(mins,"minute") << endl;
Topic archived. No new replies allowed.