Monday, July 27, 2009

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

This is the program:





#include %26lt;iostream.h%26gt;


#include %26lt;time.h%26gt;





using namespace std;





const char *get_greet();





int main(int argc, char **argv)


{


if(argc%26lt;2)


{


cout %26lt;%26lt;"please enter name" %26lt;%26lt; endl;


}


cout %26lt;%26lt; get_greet() %26lt;%26lt; " " %26lt;%26lt; argv[1] %26lt;%26lt; "!" %26lt;%26lt; 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

C++ program help - using %26lt;time.h%26gt; - answer within 6 hours please!?
Looks to me like get_greet returns a greeting specific to the time of day, so if hours %26lt; 12 it would be "Good Morning" or if hours%26gt;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 %26lt;%26lt; get_greet(buffer, SOME_SIZE) %26lt;%26lt; " " %26lt;%26lt; argv[1] %26lt;%26lt; "!" %26lt;%26lt; 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?
Reply:Don't get me wrong here. But doing these yourself are excellent ways to begin to understand C/C++ functions.

survey monkey

No comments:

Post a Comment