0

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
:)

4
Contributors
4
Replies
7
Views
4 Years
Discussion Span
Last Post by Dudearoo
Featured Replies
  • Just replace the line 15 with this: `cout << space << '\n';` Read More

  • Actually, there's still a problem with it; the [formatting string](http://www.cplusplus.com/reference/clibrary/ctime/strftime/) 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 … Read More

2

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;
}
Votes + Comments
good catch :)
1

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);
}
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.