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

Recommended Answers

All 3 Replies

In C++

day = 'p'

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

day == 'p'

See line 57

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

D:

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.

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.