Returning date string from function

Hi!!
I'm trying to return a string of charachters from a function.
bufferF in Fdato() function prints the correct date on the screen.
I need this date to be avalible outside the function.
temp only writes strange charachters.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "time.h"
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <fstream>
using namespace std;

char *Fdato(char *tempdate)

char *temp;
char buf[100];
int main(int argc, char *argv[])
{
	temp = Fdato("01/01/2000");
	cout << temp;

return 0;
}

char *Fdato(char *tempdate)
{
	time_t rawtime;
	struct tm * timeinfo;
	char bufferF[11];

	time ( &rawtime );
	timeinfo = localtime ( &rawtime );
	
	strftime (bufferF,11,"%d/%m/%Y",timeinfo);
	
	puts(bufferF);
	tempdate = bufferF;

	return bufferF;
}



Instead of going through all of this, (which I am not sure if you are asking this specifically for a school project) you could always write the time to the string stream. Therefore it would be available outside your function.

#include <sstream>

Look this up, should simplify your whole program a good bit. Writing to the S stream will give you the option of using it outside your function. Passing pointers is never fun!!
Your char buffer is a local var. It gets deleted when exiting the function. Use std::string or use new. I recommend using std::string.
Topic archived. No new replies allowed.