954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

COnverting military time to standard time

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
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:"<>hours;

cout<<"Enter the time in 24 hours military standard minutes:"<>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:"<12 && h<=24)
{
cout<<"The time in 12 hour format is:"<

ninthwondernj
Newbie Poster
1 post since Oct 2004
Reputation Points: 10
Solved Threads: 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;
}
}
cosi
Junior Poster
153 posts since Aug 2004
Reputation Points: 17
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You