I am supposed to create a program like this, with separate clock.cpp, clock.h, and main.cpp files. The program is supposed to do these tasks: I have started the clock.cpp and dont quite know where to go for some of the tasks, and I don't quite know how to use the time.h library

* Setting the Clock's current time by specifying hours, minutes as arguments
* Setting the Clock's current time by reading the time from the operating
system. Use the time() and localtime() calls for this: see "man 2 time", "man 3
localtime". ( Any suggestions on how to go about doing this? )
* Being configured as either a 24-hour or am/pm clock (picture a switch on the
back of a clock that changes its display mode)
* Printing the Clock's current time
* Getting the Clocks' current time (returning hours, minutes as output
parameters -- this is different than printing the current time) (Any suggestions on how to do this? )
* Comparing the time represented in two Clocks using operator<. ( havent attempted this but I don't see it as being so difficult )

#include <time.h>
#include <iostream>
#include <sstream>
using namespace std;

class Clock{


  typedef struct{
    int hours;
    int minutes;
    bool military;
    bool ampm;
  } time;

  time setTime( time A ){
    cout << "Enter Hours:";
    cin >> A.hours;
    cout << "\nEnter Minutes:";
    cin >> A.minutes;
    cout << "\nEnter 1 for a twelve hour clock, 0 for 24";
    cin >> A.military;
    if( A.military ){
      if( A.hours > 12 ){
        A.hours = A.hours - 12;
        A.ampm = 1;
  void printTime( time A ){
    cout << "The current time is:" << A.hours << ":" << A.minutes << endl;


To be honest, i havent created the main program yet, but will do that shortly and will have more questions, Im just seeing what kind of input i get for this program so far.

Recommended Answers

All 5 Replies

The functions in time.h aren't at all difficult to use -- you just need to read about the different functions.

time() -- returns the current time in seconds since 1970. It returns the time in an unsigned int (size_t) variable. When you have to get the current time this is the first function you want to call.

localtime() -- takes the return from time() and converts it to a struct tm structure.

time_t now = time(0); // get time in seconds
struct tm* tm = localtime(&now);

From there you will easily see how to extract the hour and minutes from the struct tm structure. If you know how to use structures and pointers then this will be a snap for you.

so i basically re did the files, and I'm getting the error time does not name a type... heres my code for the .h and .cpp


#include <time.h>
#include <iostream>

#include "Clock.h"

using namespace std;

  hours = minutes = 0;

time::time( int hours2, int minutes2 ){

  hours = hours2;
  minutes = minutes2;

int time::getHours() const{
  return hours;

int time::getMinutes() const{

  return minutes;

void time::setHours(const int hours2){

  hours = hours2;

void time::setMinutes( const int minutes2){

  minutes = minutes2;

time time::operator+(const time &t1) const{               //Error points to this line

  time sum;

  sum.minutes = minutes + t1.minutes;
  sum.hours = hours + t1.hours + sum.minutes/60;
  sum.minutes %= 60;

  return sum;

void time::show() const{


#include <time.h>
#include <iostream>
using namespace std;
#ifndef _Clock_H_
#define _Clock_H_

class time{

  int hours, minutes;


  time( int hours2, int minutes2 );

  int getHours() const;
  int getMinutes() const;
  void setHours( const int hours2 );
  void setMinutes( const int minutes2 );
  time operator+( const time &t1 ) const;
  void show() const;


so after working on this program for a while i think im getting somewhere, but im getting the error message:

Clock.h:12: error: expected constructor, destructor, or type conversion before ‘using’

any idea why i would be getting an error for

using namespace std;

time is a reserved identifier.

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.