2

Following code outputs the values in time format, i.e. if it's 1:50pm and 8 seconds, it would output it as 01:50:08

cout << "time remaining: %02d::%02d::%02" << hr << mins << secs;

But what I want to do is (a) convert these ints to char/string (b) and then add the same time format to its corresponding char/string value.

I have already achieved (a), I just want to achieve (b).

e.g.

    char currenthour[10] = { 0 }, currentmins[10] = { 0 }, currentsecs[10] = { 0 };

    itoa(hr, currenthour, 10);
    itoa(mins, currentmins, 10);
    itoa(secs, currentsecs, 10);

Now if I output 'currenthour', 'currentmins' and 'currentsecs', it will output the same example time as, 1:50:8, instead of 01:50:08.

Ideas?

Daqs
  • 720
  • 3
  • 8
  • 28
  • 1
    Your first cout statement works? it doesn't here: http://coliru.stacked-crooked.com/a/47f11bcdd5421c21 – NathanOliver Feb 24 '16 at 16:57
  • would it be easier if you convert the values to time_t first? – user3528438 Feb 24 '16 at 16:58
  • I assumed, using %02 was a standard c/c++ practice. Am I wrong? I'm using CryEngine and this statement works there, but instead of cout, CryEngine has a different function. For referece, this works in CryEngine: CryLog("time remaining: %02d::%02d::%02", hr, min, sec); – Daqs Feb 24 '16 at 17:00
  • I love good old `std::printf`… – styko Feb 24 '16 at 17:01
  • Ah. No that is not the same. It is like `printf`. `printf` and `cout` are completely different beast. – NathanOliver Feb 24 '16 at 17:01
  • NathanOliver: I know, that's why I am posting a (perhaps) useless comment and not an answer. – styko Feb 24 '16 at 17:02
  • @styko My comment was direct to the OP's comment before your. We posted at just about the exact same time so your comment was there. – NathanOliver Feb 24 '16 at 17:10
  • Since you are using C++ it would be much better using `std::string` and [`std::stoi`](http://en.cppreference.com/w/cpp/string/basic_string/stol) or [`std::to_string`](http://en.cppreference.com/w/cpp/string/basic_string/to_string). – Quest Feb 24 '16 at 18:09

3 Answers3

7

If you don't mind the overhead you can use a std::stringstream

#include <sstream>
#include <iomanip>

std::string to_format(const int number) {
    std::stringstream ss;
    ss << std::setw(2) << std::setfill('0') << number;
    return ss.str();
}
Haatschii
  • 9,021
  • 10
  • 58
  • 95
  • 1
    I would change your opening sentence to: ""Use a 'std::stringstream'". Performance should only be a concern once the software works correctly but doesn't meet its timing constraints. – RichN Feb 24 '16 at 17:12
  • @RichN Feel free to edit my post. Although to me the OP suggested that a rather low level implementation is used/wanted, which is why I added the performance hint. Nevertheless I completely agree to your argument. – Haatschii Feb 24 '16 at 17:23
3

As from your comment:

"I assumed, using %02 was a standard c/c++ practice. Am I wrong?"

Yes, you are wrong. Also c/c++ isn't a thing, these are different languages.

C++ std::cout doesn't support printf() like formatting strings. What you need is setw() and setfill():

cout << "time remaining: " << setfill('0')
     << setw(2) <<  hr << ':' << setw(2) << mins << ':' << setw(2) << secs;

If you want a std::string as result, you can use a std::ostringstream in the same manner:

std::ostringstream oss;
oss << setfill('0')
     << setw(2) <<  hr << ':' << setw(2) << mins << ':' << setw(2) << secs;
cout << "time remaining: " << oss.str();

Also there's a boost library boost::format available, that resembles the format string/place holder syntax.

Community
  • 1
  • 1
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
0

As an alternative to IOStreams suggested in other answers, you can also use a safe printf implementation such as the fmt library:

fmt::printf("time remaining: %02d::%02d::%02d", hr, mins, secs);

It supports both printf and Python-like format string syntax where type specifiers can be omitted:

fmt::printf("time remaining: {:02}::{:02}::{:02}", hr, mins, secs);

Disclaimer: I'm the author of fmt.

vitaut
  • 49,672
  • 25
  • 199
  • 336