简体   繁体   中英

time_t (mhour) operations (+ / - …)

thank's for read, this is easy question but ... i'am surprised.

My code is :

/* GET THE TIME */ 

time_t theTime = time(0);
struct tm *aTime = localtime(&theTime);
int mhour = aTime->tm_hour;

ostringstream oss;
string myString = " ";

oss << mhour;
myString += oss.str(); // OKAY, OUTPUT Correct

std::cout << myString << endl;    

// But if i replace and execute this : 

oss << (mhour + 3);
myString += oss.str();// OUTPUT IS EMPTY ! Why ? How can i add +, -, * on "mhour" ?

Why ? How can i add +, -, * on "mhour" ?

Where do you expect your output? You don't print anything to the console the second time around.

If you put this line of code at the end of your sample:

std::cout << myString << endl;

Then the total output of your program will be (for example):

14
141417

This makes sense, as you add oss twice to your string, and the second time it hasn't been cleared, nor has your string.

So your value of mhour is increasing as you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM