24 hour time conversion

Hi there,
This is code for converting 24hr time to 12 hr time. I'm getting an error message that disply is not being initialized, and also I haven't been able to figure out how to get a '0' to show in minutes >10. my loop is to contain only functions. Any ideas to help me out here?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*************
Program:	Asg_13.cpp
Author: 	Alan P. Matie                      
Date:       29 Apr 2009
Description: 
	Savitch, Pg 297, #1 -- Time Conversion
	Write a program that converts from 24-hour notation to 12-hour notation. 
	For example, it should convert 14:25 to 2:25 PM. The input is given as two intergers.
	There  should be at least three functions, one for input, one to do the conversion,
	and one for output. 
	Record the AM/PM information as a value of type char, 'A' for AM and 'P' for PM. 
	Thus, the function for doing  the conversions will have a call-by-reference formal
	parameter of type char to record whether it is AM of PM. (The function will have 
	other parameters as well.) Include a loop that lets the user repeat this 
	computation for new input values again and again until the user says he or she 
	wants to end the program. 
New Concepts:
Challenges:
	Add a loop, and it contains only functions!
	Functions must be of proper type and pass by value, or reference as appropriate.
	There must be functions for all tasks (input, processes, and output) or the program
	will earn a grade of zero.
Last Modified: 01 May 2009
**************/

#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;

// Declare constants


// Function Prototypes
void Welcome();
void in_time(int& hours, int& minutes);
void repeat(char& ans);
int time_convert(int& hours, int& minutes);
void printTime(int hours, int minutes, char display);



int main()
{
	// Declare variables below here
	int hours = 0, minutes, _24hr_time;
	char ans, display;
	
			
	// Initialization Section for real number output. DO NOT MOVE!
	cout <<setiosflags(ios::fixed | ios::showpoint);
	cout <<setprecision(2);
	// Begin your "main processing" below here
	Welcome();
	do
	{
		in_time(_24hr_time, minutes);
		time_convert(_24hr_time, minutes);
		printTime(_24hr_time, minutes, display);
		repeat(ans);
	}while (toupper(ans) == 'Y');

	

	
	return 0;
}

// function definitions below here
void Welcome()
{
	cout << "Welcome to your 24 hour to 12 hour time conversion program!"<<endl<<endl;
}
void in_time(int& hours, int& minutes)
{
	cout<< "Please enter the hour portion of the time to be converted ==> ";
	cin >> hours;
	cout<< "Please enter the minutes portion of the time to be converted ==> ";
	cin >> minutes;
}
int time_convert (int& hours, int& minutes)
{
	if (hours > 12)
	{
		hours = hours - 12;
		display = 'P';
	}
	else
	{
		display = 'A';
	}
	return 0;
}

void printTime(int hours, int minutes,char display)
{
	cout<< "The time converted to a 12 hour format is: "<<hours<<":"<<minutes<<""<<display<<"M"<<endl;
}
void repeat(char& ans)
{
	cout << "Would you like to run another conversion? Y or N ?"<<endl;
	cin >> ans;
}
That's because you are declaring display as a variable local to main(), then trying to use it out of scope in time_convert(). In order to use it the way you are, you would need to make display a global variable or pass it as a parameter.

I would just put a check for whether minutes is less than 10, and if so, manually add the zero. Something like (pardon all the parentheses):
std::cout<<((minutes < 10)?("0"<<minutes):(minutes))<<std::endl;

EDIT: Syntax error in my example fixed...
Last edited on
Thanks for the response but I am going about it wrong and need to rewrite anyway. I'm supposed to be trying :
Record the AM/PM information as a value of type char, 'A' for AM and 'P' for PM.
Thus, the function for doing the conversions will have a call-by-reference formal
parameter of type char to record whether it is AM of PM.
Unfortunately I don't understand it, and I better go reread this chapter.
Thus, the function for doing the conversions will have a call-by-reference formal
parameter of type char to record whether it is AM of PM.
Yes... Or you could just, you know, return a char. Your time_convert() is currently using its return value for nothing.
Topic archived. No new replies allowed.