| | |
program help
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2004
Posts: 1
Reputation:
Solved Threads: 0
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[]);
}
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[]);
}
•
•
Join Date: May 2004
Posts: 92
Reputation:
Solved Threads: 9
•
•
•
•
Originally Posted by mike3x1
...
For the void set how would i set the char mer[] to am or pm.
...
C Syntax (Toggle Plain Text)
#include <stdio.h> void Set(char string_time[]) { sprintf(meridian, string_time); }
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.
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!
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!
•
•
•
•
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.
C Syntax (Toggle Plain Text)
class time { private: int hour, minute; public: void set(int h, int m, bool am); void display(); }; void time::set(int h, int m, bool am) { minute = m; if am hour=h; else hour=h+12; } void time::display() { if(hour<13) cout<<hour<<":"<<minute<<" am"<<endl; //or whatver output function you use else cout<<hour-12<<":"<<minutes<<" pm"<<endl; }
![]() |
Similar Threads
- Playing .Wav/MIDI files in a Visual Basic Program (Visual Basic 4 / 5 / 6)
- What's the HARDEST program you've written? (Computer Science)
- Cool little Program to disable startup programs (Windows NT / 2000 / XP)
- Program is shutting down right after program is executed (C++)
- 3d Program (Game Development)
Other Threads in the C Forum
- Previous Thread: How to use alloc's memory allocation functions?
- Next Thread: Recursive prime number f(x)
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o ide inches infiniteloop initialization interest kilometer km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape single socketprograming socketprogramming spoonfeeding stack standard strchr string strings structures suggestions system systemcall test testautomation unix user voidmain() wab win32api windows.h





