program help

Reply

Join Date: Jun 2004
Posts: 1
Reputation: mike3x1 is an unknown quantity at this point 
Solved Threads: 0
mike3x1 mike3x1 is offline Offline
Newbie Poster

program help

 
1
  #1
Jun 16th, 2004
Hi
i'm having a few problems..i'm trying to do a time class
that holds the hr, min , and meridian( am or Pm).
this is what i have so far. For the void set how would i set the char mer[] to am or pm.
class Time
{
long hr, min;
char meridian[5];

public:

void Set(long h, long m, char mer[]);
void Set(char string_time[]);

}
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 81
Reputation: gusano79 is on a distinguished road 
Solved Threads: 4
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: program help

 
1
  #2
Jun 17th, 2004
Originally Posted by mike3x1
...
For the void set how would i set the char mer[] to am or pm.
...
The sprintf function should work for that:
  1. #include <stdio.h>
  2.  
  3. void Set(char string_time[])
  4. {
  5. sprintf(meridian, string_time);
  6. }
If you do this with string_time larger than meridian, it will crash and burn horribly.

I recommend that you don't store and manipulate that value as a char array. If it's only going to ever have two distinct values, it's a lot easier to handle if you use a simpler type, like an int, for example. Just decide on a meaning for the simpler variable, e.g. a zero int value means "AM" and nonzero means "PM". If you're writing C++, then a bool is even better--it only has two possible values. The only time you need text is when you're displaying the time for a human being, which can be done in a separate function that interprets the numeric values and prints out the textual meaning. This will make your code simpler and easier to work with.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 34
Reputation: Fili is an unknown quantity at this point 
Solved Threads: 0
Fili's Avatar
Fili Fili is offline Offline
Light Poster

Re: program help

 
1
  #3
Jun 18th, 2004
There is a time structure you know-- see gettime() : :lol:
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 81
Reputation: gusano79 is on a distinguished road 
Solved Threads: 4
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: program help

 
0
  #4
Jun 18th, 2004
There certainly is... Using the library functions as much as possible is good (code reuse, more sleep)--but it can be quite educational to write your own as well. It builds character, puts hair on your eyeballs, that sort of thing.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: program help

 
0
  #5
Jun 18th, 2004
It depends on what you want to DO with this class, of course, but if it were me I'd either make meridian stored as a bool or just store the time as 24 hour time.

Say your class was like this:

class ATimeOfDay
{
int hours, minutes;
bool AM;

public:

// meridian had better be "am" or "pm" or "AM" or "PM" or the like!
// hours can be 0..12, minutes 0..59.
// anything else returns false.
bool SetTime( int hours, int minutes, const char* meridian );

// here hours can be 0..23 and minutes 0..59.
// hours >= 12 implies 'pm'
bool Set24HourTime( int hours, int minutes );

// initialize in constructor
ATimeOfDay();
};

So, the advantage of a bool for AM means you don't have to constantly test against some string. You can say:

if (AM) printf("It is the morning\n");

rather than

if (strcmp(meridian,"am")==0) printf("It is the morning\n");

Just a suggestion!
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 6
Reputation: K-1 is an unknown quantity at this point 
Solved Threads: 0
K-1's Avatar
K-1 K-1 is offline Offline
Newbie Poster

Re: program help

 
0
  #6
Jun 24th, 2004
Originally Posted by mike3x1
Hi
i'm having a few problems..i'm trying to do a time class
that holds the hr, min , and meridian( am or Pm).
this is what i have so far. For the void set how would i set the char mer[] to am or pm.
  1. class time
  2. {
  3. private:
  4. int hour, minute;
  5. public:
  6. void set(int h, int m, bool am);
  7. void display();
  8. };
  9.  
  10. void time::set(int h, int m, bool am)
  11. {
  12. minute = m;
  13. if am
  14. hour=h;
  15. else
  16. hour=h+12;
  17. }
  18. void time::display()
  19. {
  20. if(hour<13)
  21. cout<<hour<<":"<<minute<<" am"<<endl; //or whatver output function you use
  22. else
  23. cout<<hour-12<<":"<<minutes<<" pm"<<endl;
  24. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC