Ive written all of the code out and it works fine for Am times.....but when i type in a time(in military time) that is after 12 noon, the output still reads AM and not PM. Heres the code that i wrote out if someone could help me see where i went wrong, i woild appreciate it.

--------------------------------------------------------------------------

#include<iostream>
using namespace std;


void calculate(int& h, double& m);
void display(int h, double m);




int main()
{
    int hours;
    double minutes;




    cout<<"Enter the time in 24 hours military standard hours:"<<endl;
    cin>>hours;

    cout<<"Enter the time in 24 hours military standard minutes:"<<endl;
    cin>>minutes;




    calculate(hours, minutes);
    display(hours, minutes);


    return 0;
}


void calculate(int& h, double& m)
{
    if(h<=12)
    {
        h=h-0;
        m=m-0;
    }

    if(h>12 && h<=24)
    {
        h=h-12;
        m=m-0;
    }
}

void display(int h, double m)
{
    if(h<=12)
    {
        cout<<"The time in 12 hour format is:"<<h<<":"<<m<<"AM."<<endl;
    }

    if(h>12 && h<=24)
    {
        cout<<"The time in 12 hour format is:"<<h<<":"<<m<<"PM."<<endl;
    }
}

Trace the execution of your code.

// here let's say that hours = 23
calculate(hours, minutes);
    void calculate(int& h, double& m)
    ...
    if(h>12 && h<=24)
    {
    h=h-12; // Now hours = 11
    m=m-0;
    }
    }

// hours = 11
display(hours, minutes);
    void display(int h, double m)
    {
    if(h<=12) // this branch is taken sinch h = 11.
    {
    cout<<"The time in 12 hour format is:"<<h<<":"<<m<<"AM."<<endl;
    }

Do you see the problem now?


Ed

void display(int h, double m)
{
if(h<=12)
{
cout<<"The time in 12 hour format is:"<<h<<":"<<m<<"AM."<<endl;
}

if(h>12 && h<=24)
{
cout<<"The time in 12 hour format is:"<<h<<":"<<m<<"PM."<<endl;
}
}
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.