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[]);

}

Dani AI

Generated

A safer, clearer approach for : avoid raw char[] for the meridian and keep parsing, validation and conversion explicit. ’s buffer/sprintf warning is correct, and the numeric/24-hour idea mentioned by and is practical. Below is a compact, modern C++ pattern (uses std::string and an enum) that keeps behavior predictable and avoids overflow problems.

#include <string>
#include <sstream>
#include <algorithm>
#include <cctype>

enum class Meridian { AM, PM };

class Time {
    int hour = 12;   // 1..12 for 12-hour representation
    int minute = 0;
    Meridian mer = Meridian::AM;
public:
    bool set12(int h, int m, Meridian md) {
        if (h < 1 || h > 12 || m < 0 || m > 59) return false;
        hour = h; minute = m; mer = md;
        return true;
    }

    bool parse12(const std::string &s) {
        std::istringstream in(s);
        int h, m; char sep; std::string tok;
        if (!(in >> h >> sep >> m >> tok)) return false;
        for (auto &c : tok) c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
        if (tok == "AM") mer = Meridian::AM;
        else if (tok == "PM") mer = Meridian::PM;
        else return false;
        return set12(h, m, mer);
    }

    int to24Hour() const {
        if (mer == Meridian::PM && hour != 12) return hour + 12;
        if (mer == Meridian::AM && hour == 12) return 0;
        return hour;
    }
};

Key points and troubleshooting

  • Handle the two special cases: 12:00 AM maps to 00:00, 12:00 PM stays 12:00. Test those explicitly.
  • For arithmetic, store a single internal value (minutes since midnight = to24Hour()*60 + minute) — it makes comparisons, addition and subtraction trivial.
  • Validate all inputs and return false or throw on bad data; prefer std::string parsing over raw buffers to avoid overflows.
  • If you need interaction with system timestamps, consider converting to/from std::chrono types rather than rolling calendar logic yourself.

This keeps the API clear, prevents unsafe string handling, and makes conversions unambiguous for display or calculation.

Recommended Answers

All 5 Replies

...
For the void set how would i set the char mer[] to am or pm.
...

The sprintf function should work for that:

#include <stdio.h>

void Set(char string_time[])
{
    sprintf(meridian, string_time);
}

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.

There is a time structure you know-- see gettime() : :lol:

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.

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!

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 
{
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;
}
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.