Dudearoo
useing Code::Blocks

Hey you guys!
I've got an problem, i want to print the current time, simple as that.
heres my Code.

#include <iostream>
#include <stdio.h>
#include <time.h>
using namespace std;

int main()
{
    time_t rawtime;
    struct tm * timeinfo;
    char space[50];
    time (&rawtime);
    timeinfo = localtime(&rawtime);

    strftime(space,80,"Current Time:%h%m%p",timeinfo);
    cout << "Hello world!" << endl;
    return 0;
}

the Output is just the hello world! line, there are no error or warning messages show ether.
I just learnt this from Cplusplus
Or can you Guys please give me a quick teaching on how to do this and tell me how it works? If all else fails.
thanks Guys
:)

Recommended Answers

All 4 Replies

Just replace the line 15 with this: cout << space << '\n';

Actually, there's still a problem with it; the formatting string is incorrect, which means that the time string never gets created. The correct format should be "%I" for hours (12-hour format) and "%M" for minutes.

Try the following:

#include <iostream>
#include <cstdio>
#include <ctime>

using namespace std;

const int Buffer_Size = 32;

int main()
{
    time_t rawtime;
    struct tm * timeinfo;
    char space[Buffer_Size];
    time (&rawtime);
    timeinfo = localtime(&rawtime);
    strftime(space, Buffer_Size,"Current Time: %I:%M %p",timeinfo);
    cout << space << endl;
    return 0;
}
commented: good catch :) +14

This is the complete time, but I don't know if it's what you actually want:

#include <ctime>
#include <stdio.h>
#include <iostream>
using namespace std;

int main(){
    time_t current=time(0);
    cout<<"Current time is: "<<ctime(&current)<<endl;
    getchar();
    return (0);
}

thanks you guys :) ill check up on all the answers and get the code fixed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.