So here is my WORKING code.
My problem lies in the output when I enter "0" or "00" for the hour in 12 midnight. No matter what it still outputs a 0 for midnight. (I want it to convert it to standard time.) I know it is something easy that I am missing. It's written in a class structure path going .h to .cpp to main.cpp so I will post them in that order.
this is the specification file:
//Time.h - This is the specification file for the Time class.
//This file will define the Time class.
#include<iostream>
using namespace std;
#ifndef TIME_H //*if undefined...
#define TIME_H //* define with following.
class Time
{ private:
int hour, minute;
public:
//inline constructors.
//Default constuctor
Time()
{
hour = 0;
minute = 1;
}
//Parameter constructor
Time(int hr, int min)
{ hour = hr;
minute = min;
}
//additional non-inline functions.
//Setter functions
void SetTime(int, int); //allows assignment of time in hours
//and minutes to the object.
void SetHour(int); //allows assignment of hour value.
void SetMinute(int); //allows assignment of minute value.
//Getter functions
int GetHour(); //returns value of hour
int GetMinute(); //returns value of minute
void PrintTime(); //prints to the screen the time in
//standard, non-military format.
};
#endif
This is the initialization file:
//Time.cpp This is the implementation file for the Time class
//We define the functions for Time class.
#include "Time.h"
#include<iostream>
using namespace std;
//setter (mutator) functions
void Time::SetTime(int hr, int min)
{
hour = hr;
minute = min;
}
void Time::SetHour(int hr)
{
hour = hr;
}
void Time::SetMinute(int min)
{
minute = min;
}
//Getter (accessor) functions
int Time::GetHour()
{
return hour;
}
int Time::GetMinute()
{
return minute;
}
void Time::PrintTime()
{
int midnight=12;
cout<<" Please enter a value for hour in military time. "<<endl;
cin>>hour;
cout<<" You have entered: "<<hour<<endl;
cout<<" Please enter a value for minute. "<<endl;
cin>>minute;
cout<<" You have entered: "<<minute<<endl;
if(hour<12 && minute<10){
cout<< hour<<":"<<"0"<<minute<<" a.m."<<endl;
}
else if(hour<12 && minute>10){
cout<< hour<<":"<<minute<<" a.m."<<endl;
}
else if(hour>12 && minute<10){
cout<<(hour - 12)<<":"<<"0"<<minute<<" p.m."<<endl;
}
else if(hour>12 && minute>10){
cout<<(hour - 12)<<":"<<minute<<" p.m."<<endl;
}
else if(hour==0 && minute<10){
cout<<midnight<<":"<<"0"<<minute<<" a.m."<<endl;
}
else if(hour==0 && minute>10){
cout<<midnight<<":"<<minute<<"a.m."<<endl;
}
}
aaaand here is the main program:
#include "Time.cpp"
#include<iostream>
using namespace std;
int main()
{
//declaring instances of class Time
Time time1;
Time time2(5,12);
//testing functions
cout<<"time1:\n";
time1.PrintTime();
/*cout<<" Please enter a value for hour in military time. "<<endl;
cin>>hour;
cout<<" You have entered: "<<hour<<endl;
cout<<" Please enter a value for minute. "<<endl;
cin>>minute;
cout<<" You have entered: "<<minute<<endl;*/
return 0;
}
I know it's in the if, else if's in my .cpp...but I can't figure out why it isn't converting over. Thanks for the help in advance.