Romz
Romz

Reputation: 1457

std::cout shows hex on the screen instead of text

I get system time like this:

time_t t = time(0); 
struct tm* now = localtime(&t);
TCHAR* tInfo = new TCHAR[256];
swprintf_s(tInfo
    , 256
    , _T("Current time: %i:%i:%i")
    , now->tm_hour
    , now->tm_min
    , now->tm_sec);

And then show on screen:

std::cout << tInfo << std::endl; 

But insted of Current time: 12:57:56 I got: 0x001967a8 on the screen. What I did wrong ?

Upvotes: 0

Views: 246

Answers (3)

Ajay
Ajay

Reputation: 6590

C++ shares its date/time functions with C. The tm structure is probably the easiest for a C++ programmer to work with - the following prints today's date:

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

int main() {
time_t t = time(0);   // get time now
struct tm * now = localtime( & t );
cout << (now->tm_year + 1900) << '-' 
     << (now->tm_mon + 1) << '-'
     <<  now->tm_mday
     << endl;
}

Upvotes: 0

Mats Petersson
Mats Petersson

Reputation: 129324

You are trying to print a "wide" string. You need to use :

std::wcout << tInfo << std::endl;

The "narrow" version cout doesn't know about "wide" characters, so will just print the address, just like if you tried to print some other random pointer type.

Upvotes: 4

billz
billz

Reputation: 45410

Try:

std::wcout << tInfo << std::endl; 

Upvotes: 3

Related Questions