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.

Let's imagine that you entered 0 for hour, and 5 for minutes.
Which of your if statements is going to be true? The first one. This one: if(hour<12 && minute<10) So we'll enter that block.

Let's imagine that you entered 0 for hour, and 15 for minutes.
Which of your if statements is going to be true? The second one. So we'll enter that block.

How will we ever reach the blocks that apply the special handling for hour being 0?

ahh...good point. I knew it was something ridiculously elementary. Guess I need to pay better attention to my data path. I fixed it this way:

//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()
        {

            //float hourPM=hour-12;
            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==0 && minute<10){
            cout<<midnight<<":"<<"0"<<minute<<" a.m."<<endl;
            }
            else if(hour==0 && minute>10){
            cout<<midnight<<":"<<minute<<"a.m."<<endl;
            }
            else 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;
            }


        }

Thank you for your help! I would ask if there is a better way to code this but I don't want to jump ahead of my professor.

Tell me, what happens if you enter 10 for minute, or 12 for hour?

yeah...I see that now too. Give me a minute and I'll fix it...and I'll also run back through it.
Thanks

Here it is: I tried to use >= but didn't work for some reason.

//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()
        {

            //float hourPM=hour-12;
            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==0 && minute==10){
            cout<<midnight<<":"<<minute<<" a.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;
            }
            else if(hour==12 && minute==10){
            cout<<midnight<<":"<<minute<<" p.m."<<endl;
            }
            else if(hour==12 && minute==10){
            cout<<midnight<<":"<<minute<<" p.m."<<endl;
            }
            else if(hour==12 && minute==10){
            cout<<midnight<<":"<<minute<<" p.m."<<endl;
            }
            else 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;
            }


        }

Thanks a ton Moschops for the direction.

That is a LOT of if statements for a nice output. Acutally there is a quicker way to do it [ assuming that you don't want to use printf or boost format etc ]

   #include <iomanip>

   // other stuff...

   cout<<setfill('0');
   cout<<setw(2)<<hour<":"<<setw(2)<<min<<endl;
   cout<<setfill(' ');

What that does is set the fill character to be 'zero', normally it would be a space. Then it forces the output to occupy two characters.
Thus if the output is going to be 1 it will be written as 01, but if the output is going to be 12, then no problem it will be written as '12'.

You will see that I have unset the fill character on the next line as it is persistant. [and if you forget very annoying!]

Also please don't put using namespace std; in include files [I actually prefer not to ever put it], as you will include that file in someother file and then find hundreds of names that are in namespace std have just come into your global space, and when you accidentally mistype a variable etc and find that you have used one of the std names, the compiler error is a nightmare.

Second, you don't need to include "Time.cpp" in your main program, just compile and link to it.

I appreciate that. Honestly I like the simpler, less code route. My professor hasn't shown us that yet, so I'm not quite sure I understand it. I am going to add that in and play around with it so I can gain some understanding. I was told by a lab assistant to shorten it already (I literally had a full on if, else if list for all iterations of hour on a 24 hour clock.)

As for including the initialization file, my professor asked for us to do it that way. I am interested though for better understanding, how do I link to a class file without including it in the main program?

p.s.- I don't know if it matters, but we're using CentOS with RedHat 32 bit as the virtual machine. I know other IDE's have different, sometimes simpler ways of doing things, but they said they want us to do it this way, so we can appreciate the shorter routes more later on.

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.