1
#include<iostream>
#include<ctime>
#include<string>
using namespace std;

int main()
{
    string NowTime;
    time_t now;

    struct tm nowLocal;

    now=time(NULL); // get the time from the OS

    nowLocal=*localtime(&now);

    NowTime = nowLocal.tm_hour + ':' + nowLocal.tm_min + ':' + nowLocal.tm_sec;
    cout<< NowTime;
}

When I run the program, it display nothing, can someone help me? I am totally new in programming.

chun11197
  • 11
  • 5

7 Answers7

1

If you try

cout << nowLocal.tm_hour + ':' + nowLocal.tm_min + ':' + nowLocal.tm_sec;

you'll see an integer, not anything resembling a time.
This is because it's the sum of five integers - the characters are promoted to integers, and then it's all added up.

The simplest fix is to not build a string at all but to output the parts individually:

cout << nowLocal.tm_hour << ':' << nowLocal.tm_min << ':' << nowLocal.tm_sec;

Otherwise, you need to convert those numbers to strings:

NowTime = std::to_string(nowLocal.tm_hour) + ':' + std::to_string(nowLocal.tm_min) + ':' + std::to_string(nowLocal.tm_sec);

or, you can use a std::ostringstream, which works just like std::cout and other streams, but writes to a std::string:

std::ostringstream ss;
ss << nowLocal.tm_hour << ':' << nowLocal.tm_min << ':' << nowLocal.tm_sec;
NowTime = ss.str();
molbdnilo
  • 64,751
  • 3
  • 43
  • 82
0

The line:

NowTime = nowLocal.tm_hour + ':' + nowLocal.tm_min + ':' + nowLocal.tm_sec;

is adding the hour, minute, and second to the numeric value of the colon symbol, since char is implicitly coerced to an int. That value is then being interpreted as a char in the string assignment operator.

Instead, you can simply output the values directly to cout. They will be formatted appropriately by cout's stream insertion operator <<.

#include<iostream>
#include<ctime>
#include<string>
using namespace std;

int main()
{
    string NowTime;
    time_t now;

    tm nowLocal;

    now=time(NULL); // get the time from the OS

    nowLocal=*localtime(&now);

    cout << nowLocal.tm_hour << ':' << nowLocal.tm_min << ':' << nowLocal.tm_sec;

    return 0;
}

If you would instead want to store them in a string, read up about stringstreams. They have a similar syntax to cout and can make formatting strings much easier.

DeathTails
  • 468
  • 4
  • 12
0

Instead of having to put the result in a variable, you could output it like this:

cout << nowLocal.tm_hour << ':' << nowLocal.tm_min << ':' << nowLocal.tm_sec;

Live Example

Also, if you want to keep the variable, do this:

NowTime = std::to_string(nowLocal.tm_hour) + ':' + std::to_string(nowLocal.tm_min) + ':' + std::to_string(nowLocal.tm_sec);
cout << NowTime;
Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
0

Try this:

#include<iostream>
#include<ctime>
#include<string>
#include <sstream>
using namespace std;

int main()
{
string NowTime;
time_t now;

struct tm nowLocal;

now=time(NULL); // get the time from the OS

nowLocal=*localtime(&now);

stringstream s;
s<<nowLocal.tm_hour;
s<<":";
s<<nowLocal.tm_min;
s<<":";
s<<nowLocal.tm_sec;
NowTime = s.str();
cout<< NowTime;
}

You cannot cas directly from int to string and you need put values into stream and then to string.

M. S.
  • 109
  • 7
0

What about using iostringstream to build the string you want?

#include<iostream>
#include<ctime>
#include<string>
#include <sstream>

using namespace std;

int main()
{
    ostringstream NowTime;
    time_t now;

    struct tm nowLocal;

    now=time(NULL); // get the time from the OS

    nowLocal=*localtime(&now);

    NowTime << nowLocal.tm_hour << ":" << nowLocal.tm_min << ":" << nowLocal.tm_sec;
    cout<< NowTime.str() << endl;
}

Or for the purposes of your program you could simple use std::cout which also happens to be an output stream.

cout << nowLocal.tm_hour << ":" << nowLocal.tm_min << ":" << nowLocal.tm_sec << endl;
0

Since it looks like you're pre-c++11 and can't use std::to_string. Here's a C-like way of doing it, sticking to the includes you're currently using.

#include<iostream>
#include<ctime>
#include<string>

using namespace std;

#define STR_LEN 128

int main()
{
    string nowTime;

    time_t now;

    struct tm nowLocal;

    now = time( NULL ); // get the time from the OS

    nowLocal = *localtime( &now );

    char hour[ STR_LEN ], min[ STR_LEN ], sec[ STR_LEN ];

    sprintf( hour, "%d", nowLocal.tm_hour );

    sprintf( min, "%d", nowLocal.tm_min );

    sprintf( sec, "%d", nowLocal.tm_sec );

    nowTime = string( hour ) + ':' + string( min ) + ':' + string( sec );

    cout << nowTime << endl;
}
davepmiller
  • 2,620
  • 3
  • 33
  • 61
0

How append system time to a string?

My answer is to build a convenience function.

If you really only need hour, minute, second, then you need not use the relatively slow localtime(). (on the other hand, if you do need more, I think you should prefer localtime_r() for the conversion).

For an embedded system several contracts back, I found this conversion to be a relatively slow function and chose to avoid it. The algorithms to handle leap days, centuries, etc. appear simple enough. I suspect I considered it slow simply because it calculates more than I needed in that application that was trying to do the conversion many times per second.

There exists a simpler (and probably still faster) approach involving modular arithmetic. It starts the same - with a time(0) (and thus I suspect what I am doing here is 'hidden' in the localtime_r() function). Side note 1 - on my older Dell running Ubuntu 15.10, time(0) is simply the fastest access to the wall clock, measuring about 6 or 7 ns 'typical' duration. Side note 2 - time_t may change someday. "The time_t Wikipedia article article sheds some light on this. The bottom line is that the type of time_t is not guaranteed in the C specification."

The code I currently use to conveniently generate a time stamp string:

std::string hhCmmCssGet(time_t tt = 0)  // tt has default value
{
   time_t  now = ( tt ? tt : time(0) );

   static time_t     prev = 0;
   static char   hhmmss[] = "hh:mm:ss";

   if (prev != now)
   {
      prev = now;

      const int32_t CST = -6;
      int64_t  hr = ((now / 3600) % 24) + CST; //  hr of day, CST offset
      if (hr < 0) hr += 24;
      uint64_t min = ((now / 60)   % 60); // min of day
      uint64_t sec =  (now % 60);         // sec of day

      std::stringstream ss;
      ss << std::dec
         << std::setfill('0')  << std::setw(2) << hr   << ":"
         << std::setfill('0')  << std::setw(2) << min  << ":"
         << std::setfill('0')  << std::setw(2) << sec;

      for (size_t i=0; i<8; i++)  // transfer new value
         hhmmss[i] = ss.str()[i]; // into static hhmmss
   }
   std::string retVal(hhmmss);
   return(retVal);           
}

The static items and "if (prev != now)" clause, allow this function to be invoked thousands of times per second ... with much reduced effort. The second, after all, only updates 1ce per second. And note that the std::stringstream stuff and modular arithmetic operations only run 1ce per second.

2785528
  • 5,438
  • 2
  • 18
  • 20