Hi all.

I am having trouble with my code. I have played around with it for a while, and I just can't seem to get it. The only thing I am having trouble with is it getting to output the a.m. and the p.m. correctly. it always outputs p.m. no matter what the input is

I am a student, and I am learning. The chapter this week is using value vs reference parameters.

#include<iostream>
#include<iomanip>

using namespace std;

//function prototypes
void getInput(int& hour, char& ch, int& minutes);
void convertTo12(int& hour);
void printOutput(int& hour, char ch, int minutes, char& day);

//begin main
int main()
{
	int hour;
	char ch;
	int minutes;
	char day;

	getInput(hour, ch, minutes);
	convertTo12(hour);
	printOutput(hour, ch, minutes, day);


	system("pause");
	return 0;
}//end main

//beging getinput. Get's the input from the user.
void getInput(int& hour, char& ch, int& minutes)
{
	
	cout << "Please enter the time in 24 hour format." << endl;
	cin >> hour >> ch >> minutes;
}// end getIntput

//begin converTo12. Converts the input to 12 hour format and
// outputs a or p depending whether it is a.m. or p.m. this 
// function does not output the am or pm to the screen.
void convertTo12(int& hour)
{
	char day;
	if (hour >= 12)
		day = 'p';
	else
		day = 'a';

	if(hour >= 13)
	{
		hour = hour - 12;
	}
}//end Converto12

// begin printOutput. Prints the output to the screen
void printOutput(int& hour, char ch, int minutes, char& day)
{
	cout << hour << ch << minutes;
	if (day = 'p')
		cout << "p.m.";
	else
		cout << "a.m.";
}//end printOutput

In C++

day = 'p'

is the assignment operator not the comparison operator...You want

day == 'p'

See line 57

Edited 6 Years Ago by gerard4143: n/a

Whoops. I knew that. But now it won't stop outputting a.m.

D:

Your function

void convertTo12(int& hour)

only changes the am pm character locally. You need to pass the character as a reference.

This article has been dead for over six months. Start a new discussion instead.