Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++ program help - using <time.h> - answer within 6 hours please!?

This is the program:

#include <iostream.h>

#include <time.h>

using namespace std;

const char *get_greet();

int main(int argc, char **argv)

{

if(argc<2)

{

cout <<"please enter name" << endl;

}

cout << get_greet() << " " << argv[1] << "!" << endl;

return 0;

}

The questions are:

1. Write pseudo code illustrating the logic you will have in the function get_greet

2. If definition of get_greet was "void get_greet(char *buffer, int size)", what are the changes you have to do to main() to get it working?

Any help is appreciated.

Thank you so much

Update:

This is NOT homework. And it is not any sort of schoolwork/tutorial.

I have already tried it out, but I get an error. I checked documentation, but that was no help.

2 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    Looks to me like get_greet returns a greeting specific to the time of day, so if hours < 12 it would be "Good Morning" or if hours>18 "Good Evening." Why don't you open up your time.h header file or look up the documentation elsewhere (I use GCC. They kind of EXPECT you will open and read your header files if you want to use them properly).

    The answer to number two seems obvious but if this is homework believe me, we are doing you no favor by answering that question.

    EDIT: Okay, the point would be you would want to extract hours from an asctime() string. I've added a discussion of it in sources. The asctime() string is a fixed-length string, so you simply go to the place where hours start, extract that and the next char, convert them to an integer (probably using itoa() which means storing them in a 3-char string and making the third char '\0') and going from there.

    The answer to your second question of course is in theory you can just do a cout << get_greet(buffer, SOME_SIZE) << " " << argv[1] << "!" << endl; assuming SOME_SIZE is #defined up above as the maximum number of chars to put in buffer and buffer is defined as a block of memory which is large enough to hold enough chars. {Incidently, I tend to send printf and cout ' ' and '!' rather than " " and "!" because when you use single quotes it sends chars but when you use double quotes it sends a two char array with the second char being'\0'. I know it doesn't save a lot but I've been questioned about why my code compiles smaller than some other peoples}.

    You CAN do the above. It will evaluate get_greet(buffer, SOME_SIZE). It may be easier for you if you execute that above the cout statement and then just send it buffer.

    Again, you either have to declare buffer as an array of fixed size, or allocate enough memory using malloc and remember to free it before exiting. And of course you can send get_greet a variable or a const or a digit, but the advantages of #defining a SOME_SIZE are if you have to use it elsewhere in the program you change it once -- while the program gets no chance to change it as the preprocessor will substitute whatever number is there for every occurance of SOME_SIZE, and with the right name people will know exactly what it is for.

    Is that helpful?

  • P34C3
    Lv 4
    1 decade ago

    Don't get me wrong here. But doing these yourself are excellent ways to begin to understand C/C++ functions.

Still have questions? Get your answers by asking now.