Error C2040 varied levels of inderection

Currently i'm trying to get the date using the localtime_s function but i'm getting an error. The problematic part of my code is

time_t rawtime;
struct tm timeinfo;
localtime_s(&timeinfo, &rawtime);

This isn't doing anything yet, i'm assuming there's something I need to add to actually reference timeinfo i just don't know what it is?
Your parameters are in the wrong order.
struct tm *localtime_s(const time_t *restrict time, struct tm *restrict result);
http://en.cppreference.com/w/c/chrono/localtime
It's more likely that the OP is using Microsoft's localtime_s:
errno_t localtime_s(struct tm* _tm, const time_t *time);
https://msdn.microsoft.com/en-us/library/a442x3ye.aspx <- this by the way gives a usage example

(C's localtime_s adopted the function signature from POSIX localtime_r)
Last edited on
The code now runs but the output is "-1/0/1969" rather than the current date.
The rest of the code is below:



time_t rawtime;
struct tm timeinfo;
localtime_s(&timeinfo, &rawtime);

int month = 1 + timeinfo.tm_mon,
day = timeinfo.tm_mday,
year = 1970 + timeinfo.tm_year;


string currentDate = to_string(month) + "/" + to_string(day) + "/" + to_string(year);
This should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream> 
#include <ctime>

using namespace std;

int main () 
{
time_t rawtime = time(0);
struct tm timeinfo;

localtime_s(&timeinfo, &rawtime);

int month = 1 + timeinfo.tm_mon,
day = timeinfo.tm_mday,
year = 1970 + timeinfo.tm_year;

cout << timeinfo.tm_mday << "-" << 1 + timeinfo.tm_mon << "-" << timeinfo.tm_year + 1900;

return 0;
}


Output was 22-5-2016
Topic archived. No new replies allowed.