John B
John B

Reputation: 95

strftime adds unwanted characters to what I'm trying to display

Hey guys I'm wondering if someone can point this out to me. I'm currently trying to display the date & time in the right hand corner of the cmd prompt but I'm getting additional characters then what I'm asking for. This is what I have so far

time_t rawtime;
struct tm * timeinfo;
char buffer[80];

time(&rawtime);
timeinfo = localtime(&rawtime);

// this displays "23                    Date:etcetcetc"
cout << "\n\n\n\n\n\n\n\n\n\n\n\n";
cout << strftime(buffer, 80, "\t\t\t\t\t\t\tDate: %d/%m/%Y", timeinfo);
puts(buffer);
// and this displays "16Time:etcetcetc"
cout << "\t\t\t\t\t\t\t";
cout << strftime(buffer, 80,"Time: %I:%M:%S \n", timeinfo);;
puts(buffer);

if you can see the comments it gives me what I want but adds 23 to the first line and 16 to the other, what am I doing wrong?

Kind Regards!

JB

Upvotes: 0

Views: 123

Answers (2)

tivn
tivn

Reputation: 1923

Try replace this:

cout << strftime(buffer, 80, "\t\t\t\t\t\t\tDate: %d/%m/%Y", timeinfo);

with this:

strftime(buffer, 80, "\t\t\t\t\t\t\tDate: %d/%m/%Y", timeinfo);

Upvotes: 1

Cthulhu
Cthulhu

Reputation: 1372

cout << strftime(buffer, 80, "\t\t\t\t\t\t\tDate: %d/%m/%Y", timeinfo);

and

cout << strftime(buffer, 80,"Time: %I:%M:%S \n", timeinfo);

You're sending the results of the function strftime to stdout (which is the number of bytes it printed to the buffer).

And puts(buffer); actually outputs your buffer.

Upvotes: 1

Related Questions