Hi
I am making a function that finds what todays date is & returns it as a string to main. (string format dd/mm/yy).

My problem is I have found out how to find the day, month & year, but I cant put it into a string & return it to main. Any advice?

string date()
{

	SYSTEMTIME time;

	GetLocalTime( &time );
	int hour = time.wHour;

	if (hour > 12) hour -= 12;

	cout << time.wMonth << "/" << time.wDay << "/" << time.wYear << endl;
	cout << hour << ":" << time.wMinute << endl;

	int a = time.wDay;

// attempt to put date into a string
	std::string date("");
	date += time.wDay;
	date += "/";
	date += time.wMonth;
	date += "/";
	date += time.wYear;
	 
//attempt to just return the values but wont work, should this function be int or something else??
	return time.wDay;
	return time.wMonth;
	return time.wYear;

}

The return type for your function is string, so you need to return a single string.

Your functions return type is string and you've already built a string called 'date' in your code, which contains the date..
All you need to do is get rid of the three return statements and return the date variable instead.

NOTE: A function can only return one value, so the only thing this would return (if this code even compiles!) is time.wday. As soon as the function reaches the first return, it will return the first value, the other two will never be reached!

As mentioned, what you need to do is return date instead..
i.e.

return date;

Hope that clears things up for you!
Cheers for now,
Jas.

p.s. the bit of code where you're building the date string could be written like this:

// attempt to put date into a string
	std::string date = time.wDay + "/" + time.wMonth + "/" + time.wYear;

Which is a little more efficient. (I don't mean perfomance-wise, but in the amount of typing required..i.e. one line of code instead of six!)

Thanks for the advice but I have tried that, it returns the string perfectly but its jiberish.

an example of the output of string date is:

//Ù

OK, at this point I'll admit I've not used SYSTEMTIME before, I usually use ctime instead. So I'm not familiar with it!

But, the code looks syntactically sound, so if it doesn't work it's probably worth looking at the datatypes used in the SYSTEMTIME structure, as they are probably being stored in a format that would require casting or explicit conversion!

From a quick look in VS it would appear that they are all of type WORD, which is typedefined as unsigned short if memory serves....{checking..}....Yup!

What I'd recommend is converting 'time.wday' et al to strings. Casting them should probably work.

Have you tried this?

// attempt to put date into a string
	std::string date = (std::string)time.wDay + "/" + (std::string)time.wMonth + "/" + (std::string)time.wYear;

If that doesn't work, then you'll have to find another way of converting from unsigned short int to std::string.
Unfortunately, I don't have any decent reference material at hand at the mo. So we may have to do some googling if casting doesn't work...I've got a memory like a sieve for the standard c++ function names!

Jas.

#include <sstream>
#include <string>
#include <windows.h>
using namespace std;

string date()
{
   SYSTEMTIME stime;
   GetLocalTime(&stime);
   char buf[40] = {0};
   sprintf(buf,"%02d/%02d/%04d",
        stime.wDay, stime.wMonth, stime.wYear);
  string dt = buf;
  return dt;
}
Comments
Cheers! You recscued the thread there, I was about to suggest a solution using ostringstream, but sprintf is way better!


What I'd recommend is converting 'time.wday' et al to strings. Casting them should probably work.

Have you tried this?

// attempt to put date into a string
	std::string date = (std::string)time.wDay + "/" + (std::string)time.wMonth + "/" + (std::string)time.wYear;

.

Its not possible to cast an integer to a std::string.

Its not possible to cast an integer to a std::string.

Yeah, I wasn't sure myself...
One thing's for certain...my brain is not quite in gear yet today. Time for a coffee methinks!
Doh, how can I have forgotten sprintf?? {kicking self!}

Anyways, thanks for jumping in and rescuing the thread before I made any other stupid suggestions! heh heh! :)


J.

Hi, by far the easiest way (if you are working in a windows system) is to call the date and time from cmd. To do this within your c++ program, simply add:

cout<<"The time is: ";
system("time [/t]");
cout<<"The date is: ";
system("date [mm-dd-yy] [/t]");

Comments
Doesn't even remotely answer this question from JUNE 2009!

You resurrected a thread that's been dead for 18 months to give this completely lame answer? How is the date returned as a string to main() as the O/P requested?

This article has been dead for over six months. Start a new discussion instead.