so i was supose to create a software for my class that converts the 24 hour notation to 12, but now i got really lost with it
i used the A symbol to display am and the P symbol to display pm
heres what i have so far but i cant get it to work properlly for each time, here is my code... with a little description of what its supose to do

#include <iostream>
using namespace std;

//Function Declarations
void userInput(int& initHour, int& initMinute);
void convert(int& pHour, int pMinute, char& periodDay);
void output(int pHour, int pMinute, char periodDay);

int main()
{
    //Variable declarations    
    int hour, min;		//Used to track the hour and min
    char ampm;			//Used to track if its am or pm
    char redo = 'y';
    
    while (redo == 'y' || redo == 'Y')
    {
        //Function calls
     	userInput(hour, min);     	
     	convert(hour, min, ampm);
     	output(hour, min, ampm);
     	
     	cout << "Would you like to run it again? Y/N: ";
     	cin >> redo;
   	}   	
 	
 	system("pause");
    return 0;   
} 

void userInput(int& initHour, int& initMinute)
{
	cout<< "enter the time seperated by space: ";
	cin >> initHour
		>> initMinute;
}

void convert(int& pHour, int pMinute, char& periodDay)
{
	if(pHour == 24)
	{
		pHour = 12;
		periodDay = 'A';
	}
	if (pHour == 12)
	{
		pHour = pHour - 12;
		periodDay == 'P';
	}

	if(pHour > 12 && pMinute >= 0 && pMinute <= 59)
	{
		pHour = pHour - 12;
		periodDay = 'P';
	}
	if(pHour < 12)
	{
		periodDay = 'A';
	}
}


void output(int pHour, int pMinute, char periodDay)

{
	cout << "here is the converted time: " << pHour << " : " << pMinute << periodDay << "\n";
}

thanks for helping

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

Next time please use code tags.

I don't think you have to worry about the minutes at all when you are doing your conversion.

Since you know you are entering in military time, you can make your if statement if (pHour >=0 && pHour<12) for the AM/PM portion (I see how you did them together but really separating it out into two sets of ifs, one for the hour and one for the time of day).

Then just take away 12 from any hour above 13 and take midnight as a special case (I'm sure there's some way to do it with fancy modular arithmetic but if you can isolate it on its own as a case go for it).

Just a little hiccup in the display function, if your minutes are less than 10 it lops off the leading zero. You can fix this with a setfill('0') << setw(2) modifier in your cout "chain."

Edited 3 Years Ago by happygeek: fixed formatting

Just a little hiccup in the display function, if your minutes are less than 10 it lops off the leading zero. You can fix this with a setfill('0') << setw(2) modifier in your cout "chain."

just a quick question where do you put the setfill('0') << setw(2)
becouse im still lost with this else wise i got everything fixed thanks for help

#include <iomanip> at the top
cout << "here is the converted time: " << pHour << " : " << setfill('0') << setw(2)<<pMinute << periodDay << "\n";
This article has been dead for over six months. Start a new discussion instead.