Hi there^_^,

How can I create a class from this program?

#include <iostream>
#include <ctime>
using namespace std;

int main()
{
  // Obtain the total seconds since the midnight, Jan 1, 1970
  int totalSeconds = time(0);

  // Compute the current second in the minute in the hour
  int currentSecond = totalSeconds % 60;

  // Obtain the total minutes
  int totalMinutes = totalSeconds / 60;

  // Compute the current minute in the hour
  int currentMinute = totalMinutes % 60;

  // Obtain the total hours
  long totalHours = totalMinutes / 60;

  // Compute the current hour
  int currentHour = (int)(totalHours % 24);

  // Display results
  cout << "Current time is " << currentHour << ":"
    << currentMinute << ":" << currentSecond << " GMT" << endl;

  return 0;
}

I made many attempts to do so but they all did not work. Here is the latest:

#include <iostream>
#include <ctime>
using namespace std;

class Time
{
private:
  int hour;
  int minute;
  int second;

public:
  Time()
  {
    second = time(0);
  }

  Time(int newSecond)
  {
    setTime(newSecond);
  }

  int getHour()
  {
    return hour;
  }

  int getMinute()
  {
    return minute;
  }

  int getSecond()
  {
    return second;
  }

  void setTime(int newSecond)
  {
    second = newSecond % 60;

    int totalMinutes = newSecond / 60;
    minute = totalMinutes % 60;

    int totalHours = totalMinutes / 60;
    hour = totalHours % 24;
  }
};

int main()
{
  Time time1;
  Time time2(123456);

  cout << time1.getHour() << ":" << time1.getMinute() << ":" << time1.getSecond() << endl;
  cout << time2.getHour() << ":" << time2.getMinute() << ":" << time2.getSecond() << endl;

  return 0;
}

My results are:
5138931:6233464:1195994306
10:17:36

But they should be:
(whatever the current time is)
10:17:36

So, I am getting the second part correct but not the first part.

I also have these special rules (click on the following link):
http://i152.photobucket.com/albums/s183/beauty7777/cinstruct.jpg

NOTE: I am not asking for the straight out answer but for some hints and tips on what to do.

I appreciate your help.^_^

YAAAY!^_^ I think I got it!

For all who would like to know my solution:

#include <iostream>
#include <ctime>
using namespace std;

class Time
{
private:
  int hour;
  int minute;
  int second;

public:
  Time()
  {
    int newSecond;
    newSecond = time(0);
    setTime(newSecond);
  }

  Time(int newSecond)
  {
    setTime(newSecond);
  }

  int getHour()
  {
    return hour;
  }

  int getMinute()
  {
    return minute;
  }

  int getSecond()
  {
    return second;
  }

  void setTime(int newSecond)
  {

    second = newSecond % 60;

    int totalMinutes = newSecond / 60;
    minute = totalMinutes % 60;

    int totalHours = totalMinutes / 60;
    hour = totalHours % 24;
  }
};

int main()
{
  Time time1;
  Time time2(123456);

  cout << time1.getHour() << ":" << time1.getMinute() << ":" << time1.getSecond() << endl;
  cout << time2.getHour() << ":" << time2.getMinute() << ":" << time2.getSecond() << endl;

  return 0;
}

bye for now!^_^

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.