Hi guys, uve been workin on a restaurant program and im tryin to write the time to the end of the receipt. so im writing information to a file. only problem is i cannot make my time function return the current time. I've tried everything. can you guys help??

im using:

char Time::currentTime()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  return asctime (timeinfo);
}

and in the other class im usin to write the time:

list_of_orders <<time->currentTime()<<endl;

is this correct

Dani AI

Generated

A few separate problems are visible in the thread: a return-type/signature mismatch, copying into an uninitialised char*, and an identifier collision with the C library time function that produces the compiler error in Post #6. and were right to flag the signature issue, and 's suggestion to avoid naming an object time is on target — the error message means the compiler resolves time to the C time() function, not an instance with a printTime() method.

A simple, safe approach is to return a C++ string and format the timestamp with the standard library (no manual strcpy, no static buffer). This avoids lifetime and thread-safety issues and removes ownership headaches. Example pattern:

#include <chrono>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <string>

std::string currentTimeString()
{
    using namespace std::chrono;
    auto now = system_clock::now();
    std::time_t t = system_clock::to_time_t(now);
    std::tm tm = *std::localtime(&t); // use localtime_r/localtime_s if threads matter
    std::ostringstream oss;
    oss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
    return oss.str();
}

Usage from the order-creation code becomes straightforward: list_of_orders << "Time: " << currentTimeString() << '\n'; If C-style buffers are preferred, allocate a buffer and use std::strftime into it; do not strcpy into an uninitialised pointer. Finally, rename any variable named time (for example to timeObj or clock) so it does not shadow the standard library function. This combination fixes the mismatch, eliminates undefined behaviour, and resolves the compiler complaint.

Recommended Answers

All 6 Replies

Returning a pointer to char is different from returning a char.

You might want to try the function

gettimeoftheday()

PS I am not sure of the arguments

commented: Hey! Wow! This post is almost entirely useless! -2

You might want to try the function

gettimeoftheday()

PS I am not sure of the arguments

But changing the name of the function will just gimme the same result

Didn't you see the first reply to your post?

returns a character pointer.

The currentTime function returns a character.

There is a mismatch here that needs to be fixed or taken into consideration.

ok guys ive been at this for a while now and vie got a new error:

int Create::createOrder(int room_no_food_order, int set_meal_choice, int side_order)
{
    char* get_time;
    get_time = time.printTime();
    .
    . 
    .
    .
    list_of_orders << "Time:"<<get_time<<endl;
}

and in my class to create the time i have:

char* CTime::printTime()
{
    time_t rawtime;
    char* str2;
    strcpy (str2,asctime( localtime( &currentTime)));
    return str2;
}

now i get the following compiler error pointing to the second line(get_time = time.printTime();)

the error is....

error: request for member `printTime' in `time', which is of non-class type `time_t ()(time_t*)'|

any ideas???? thanks to all who help...
_________________________________________________-
discussions like this is what keep the programming world go round!!!

time is a key word. So the compiler is not allowing you to call the print time function. Change the name of the object from time to time_1 and then try

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.