c++ C++ (Question) setprecision & if else if
3 minutes ago, TheGosuStandard said:I was trying to get the time between the range of 1-59 depending on current time (mins) that is retrieved from ctime & sunset time (mins_set) which was a user input, thank you for clarifying that it cannot be both.
I meant formatting output using iomnip based on the input placed, so if I placed 30 for minutes the output would display X:30 instead of X:3, unless I am not fully understanding formatting input vs output.
You can use this to format your output whatever it is (strings, numbers):
#include <iostream> #include <iomanip> int main () { int hours, minutes; std::cin >> hours; std::cin >> minutes; std::cout << std::setfill('0') << std::setw(2) << hours << ":" << std::setfill('0') << std::setw(2) << minutes << std::endl; return 0; }
Or you can format your time so it will be HH:MM with http://www.cplusplus.com/reference/ctime/strftime/ function.
I don't know if this is what you want. But either of those will force leading zeros. Following zeros - I haven't heard anyone would need that cause it will never display 30 as 3, that is totally different number.
P.S.: Even simple check if number is > 9 and if not adding 0 before would do.

Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now