0

I'm trying to figure out how to get both military and standard time to display.

A User inputs the time, then its shown in its standard form.

It's mostly there, but the standard time portion is giving me a few issues.

#ifndef TIME_H
#define TIME_H

class Time
{
public:
    Time(int = 0, int = 0, int = 0);
    ~Time();
    int hour; // valid values are 0 to 23
    int minute; // valid values are 0 to 59
    int second; // valid values are 0 to 59
    void setTime(int, int, int); // function that checks if inputs are valid
    void printUniversal(); // prints in HH:MM:SS format
    void printStandard(); // prints in HH:MM:SS AM/PM format
    static int count; //counter
};

#endif




#include "stdafx.h"
#include "Time.h" //header file that contains the Time class file
#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;

int Time::count = 12;

Time::Time(int hr, int min, int sec)
{
    hour = hr; minute = min; second = sec;
    count++;
}


Time::~Time()
{
    count--;
}

void Time::setTime(int hr, int min, int sec)
{
    hour = (hr >= 0 && hr < 24) ? hr : 0; // checks if hour input is valid
    minute = (min >= 0 && min < 60) ? min : 0; // checks if minute input is valid
    second = (sec >= 0 && sec < 60) ? sec : 0; // checks if seconds input is valid
}



void Time :: printUniversal()
{
    cout << setfill('0') << setw(2) << hour << ":" << setw(2) << minute << ":" << setw(2) << second;
}

void Time::printStandard()
{
    cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" << setfill('0') << setw(2) << minute << ":" << setw(2) << second << (hour < 12 ? " AM" : " PM");
}


//(Where I implement the functions - main.cpp)
#include "Time.h"
#include <iostream>
using namespace std;



int main()
{
    int hour, minute, second;

    Time t; //t is the time object //(PROBLEM!!!-Dont understand how to write the correct parameters)
     //test is also a time object //(PROBLEM!!!-Dont understand how to write the correct parameters)
    //Time *tp = new Time;
    //Time *tarray = new Time[5];



    cout << "Enter hour in military time ";
    cin >> hour;
    cout << "Enter minute ";
    cin >> minute;
    cout << "Enter second ";
    cin >> second;



    cout << "\nThe standard time is ";
    t.printStandard(); //(PROBLEM!!!- I have the number 12 appearing right after AM and i can't get rid of it. )
    cout << "\nThe universal time is ";
    t.printUniversal();
    cout << endl;
    return 0;
} // end main

Update: The errors are gone for the moment, thanks to the suggestions posted.

However now when I run it, I get this....well since I can't post images yet...

I'll input 13, 45, 05 and almost like a old VCR, I can't 12:00:00 for standard or 00:00:00 for universal

No matter what I input into its all the same output.

momo
  • 1
  • 5

1 Answers1

0
Time t();     // This declares a function named t which takes 
              // no arguments and returns a Time object

Time test();  // This declares a function named test which
              // takes no arguments and returns a Time object

Time t; // this is a default constructed Time object named t
Time t( 12, 7, 5 ); // this is a Time object with all 
                    // parameters passed to the constructor

In printStandard, you have

<< sizeof(Time)

This is where the 12 is coming from. Objects of your Time class type take 12 bytes in memory.

I strongly suggest you get a book on C++, as all of this would be explained in there.

Rob K
  • 8,757
  • 2
  • 32
  • 36