-
Math Question
Hello,
I need to know how to make my program display 3:04 instead of 3:4 when you enter 184 as your minutes?
Thank You For Your Help.
/*Source Code*/
#include <iostream.h>
int main(){
int enterminutes;
double minutes;
double hours;
cout <<"Enter the number of minutes: "<<endl;
cin >> enterminutes;
hour = enterminutes/60;
minutes = (enterminutes %60);
cout <<"The time is: "<<hour<<":"<<minutes<endl;
return (0);
}
-
a combination of several output format specifiers using printf() or cout should do the trick. Otherwise you could use an if statement checking if minutes is less than 10. If so print a zero before printing the minutes, else just print the minutes.
-
Code:
if (minutes < 10)
cout << "0" << minutes;
else
cout << minutes;
Alternatively:
Code:
cout << (minutes < 0 ? "0" : "") << minutes;
You might want to also check that minutes is between 0 and 59, and add some type of error handling code if not.