-
February 22nd, 2012, 01:42 AM
#1
[RESOLVED] C++ Class Military to Standard Time
basically my program lets the user inputs military time( in hour, minute, and seconds). I need to convert this to standard time and have am/pm at the end. Here is what I have so far.
I have a 3 files, time.h, time.cpp (defines functions), and main.cpp (testing). I have a problem in my main function under main.cpp. I don't understand how to set the parameters of the two time objects; t and test. When i do test( hr, min, sec) it shows random numbers when it prints out in standard time.
Can someone help me! Thanks.
(HEADER FILE called Time.h)
#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
------------------------------------------------------------------------------------------------------
(File that contains definitions- Time.cpp)
#include <iostream>
#include <iomanip>
#include <ctime>
#include "Time.h" //header file that contains the Time class file
using namespace std;
int Time::count = 10;
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:rintStandard()
{
cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":"
<< setfill( '0' ) << setw( 2 ) << minute << ":" << setw( 2 )
<< second << ( hour < 12 ? " AM" : " PM" )<< sizeof(Time);
}
void Time:rintUniversal()
{
cout << setfill( '0' ) << setw( 2 ) << hour << ":"
<< setw( 2 ) << minute << ":" << setw( 2 ) << second;
}
---------------------------------------------------------------------------------------------------------------
(Where I implement the functions- main.cpp)
#include <iostream>
using namespace std;
#include "Time.h"
int main()
{
int hour, minute, second;
Time t(); //t is the time object //(PROBLEM!!!-Dont understand how to write the correct parameters)
Time test(); //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 << "The universal time is ";
test.printUniversal();
cout << endl;
return 0;
} // end main
Last edited by captjack; February 22nd, 2012 at 01:52 AM.
Reason: adding details
-
February 22nd, 2012, 02:10 AM
#2
Re: C++ Class Military to Standard Time
Victor Nijegorodov
-
February 22nd, 2012, 02:15 AM
#3
Re: C++ Class Military to Standard Time
Thanks VictorN for replying.
I was able to figure out the parameters and get rid of the 12 (due to the unnecessary sizeof(Time)). I made a very silly mistake and was calling the function before inputting variables.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|