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.


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);
	printOutput(hour, ch, minutes, day);

	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';
		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.";
		cout << "a.m.";
}//end printOutput
7 Years
Discussion Span
Last Post by gerard4143

In C++

day = 'p'

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

day == 'p'

See line 57

Edited by gerard4143: n/a


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


Your function

void convertTo12(int& hour)

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

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.