0

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;
    }
}

Edited by Dani: Formatting fixed

2
Contributors
1
Reply
4
Views
13 Years
Discussion Span
Last Post by cosi
0

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;
}
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.