Building my first functions

everything down to the output12 and output24 seems to work fine. I am trying to finish the conversions and output am/pm or 24hour formats.

#include <iostream>
using namespace std;

void output();
void time(int& choice);
void input(int& hours, int& minutes, char& type, int& choice, int& hours1);
void output12(int& hours, int minutes, char type, int& choice);
void output24(int& choice, int minutes, int& hours1);
int main()
{
int hours, hours1;
int minutes;
char type;
char answer;
int choice;

do {
output();
time(choice);
input(hours, minutes, type, choice, hours1);
output12(hours, minutes, type, choice);
output24(choice, minutes, hours);

cout << "\nPerform another calculation? (y/n): ";
cin >> answer;

} while ((answer == 'Y') || (answer == 'y'));
return 0;

}

void output()
{
cout << "\nTime Conversion" << endl;
}

void time(int& choice)
{
cout << "Enter the time format you want converted ";
cout << "\nEx... 12 = 8:15 P.M to 20:15";
cout << "\nex... 24 = 20:15 to 8:15 P.M.";
cout << "\n(12 or 24): ";
cin >> choice;
}

void input(int& hours, int& minutes, char& type, int& choice, int& hours1)
{
if (choice == 12)
{
cout << "Enter the hours for the 12 hour time: ";
cin >> hours;
cout << "\nEnter the minutes for the 12 hour time: ";
cin >> minutes;
cout << "\nEnter the type of time (A for A.M. or P for P.M.):";
cin >> type;
}
else if (choice == 24)
{
cout << "Enter the hours for the 24 hour time: ";
cin >> hours1;
cout << "\nEnter the minutes for the 24 hour time: ";
cin >> minutes;
}
else {
cout << "*** An error was made in your input ***" << endl;
}
}

void output12(int& hours, int minutes, char type, int& choice)
{
if (choice == 12)
{
int mil = 0;
if (hours < 12 && hours != 0 && (type == 'A' || type == 'a'))
{
mil = hours;
}
else if (hours < 12 && hours != 0)
{
mil = hours + 12;
}
else if (hours = 12 && (type == 'A' || type == 'a'))
mil = 0;
cout << "\nThe time converted to 24 hour format is: " << mil << ":" << minutes;
}

}

void output24(int& choice, int minutes, int& hours1)
{
int norStd = 0;
if (choice == 24)
{
if (hours1 <= 12 && hours1 != 0)
{
norStd = hours1;
}
else if (hours1 > 12 && hours1 < 24)
{
norStd = hours1 - 12;
}
else if (hours1 == 0)
{
norStd = 12;
}
else
{
cout << "Incorrect number entered";
}
cout << "\nThe time converted to 12 hour format is: " << norStd << ":" << minutes << " ";
if (hours1 < 12)
cout << "A.M.";
else
cout << "P.M.";
}

}
What is the problem?
The output times are not outputing in any consistent way. I am getting values not converting to AM/PM properly and the 24 hour conversion is outputting crazy numbers. it compiles ok and goes through the routine just fine but having issues with the output12 and output24 functions.
Your program doesn't work very well if you select 24 hour format and enter hour = 12.


Time Conversion
Enter the time format you want converted
Ex... 12 = 8:15 P.M to 20:15
ex... 24 = 20:15 to 8:15 P.M.
(12 or 24): 24
Enter the hours for the 24 hour time: 12

Enter the minutes for the 24 hour time: 03

The time converted to 12 hour format is: -858993460:3 A.M.
Perform another calculation? (y/n):


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
One problem is this:

void input(int& hours, int& minutes, char& type, int& choice, int& hours1)

You do not use hours1 which is the 24 value. I suggest to remove that paramter.

Another problem:

else if (hours = 12 && (type == 'A' || type == 'a'))

Use == instead of =
You were correct!!! I found it and now everything looks good.

#include <iostream>
using namespace std;

void output();
void time(int& choice);
void input(int& hours, int& minutes, char& type, int& choice, int& hours1);
void output12(int& hours, int minutes, char type, int& choice);
void output24(int& choice, int minutes, int& hours1);
int main()
{
int hours, hours1;
int minutes;
char type;
char answer;
int choice;

do {
output();
time(choice);
input(hours, minutes, type, choice, hours1);
output12(hours, minutes, type, choice);
output24(choice, minutes, hours1);

cout << "\n\nPerform another calculation? (y/n): ";
cin >> answer;

} while ((answer == 'Y') || (answer == 'y'));
return 0;

}

void output()
{
cout << "\nTime Conversion" << endl;
}

void time(int& choice)
{
cout << "Enter the time format you want converted ";
cout << "\nEx... 12 = 8:15 P.M to 20:15";
cout << "\nex... 24 = 20:15 to 8:15 P.M.";
cout << "\n(12 or 24): ";
cin >> choice;
}

void input(int& hours, int& minutes, char& type, int& choice, int& hours1)
{
if (choice == 12)
{
cout << "Enter the hours for the 12 hour time: ";
cin >> hours;
cout << "Enter the minutes for the 12 hour time: ";
cin >> minutes;
cout << "Enter the type of time (A for A.M. or P for P.M.):";
cin >> type;
}
else if (choice == 24)
{
cout << "Enter the hours for the 24 hour time: ";
cin >> hours1;
cout << "Enter the minutes for the 24 hour time: ";
cin >> minutes;
}
else {
cout << "*** An error was made in your input ***" << endl;
}
}

void output12(int& hours, int minutes, char type, int& choice)
{
if (choice == 12)
{
int mil = 0;
if (hours < 12 && hours != 0 && (type == 'A' || type == 'a'))
{
mil = hours;
}
else if (hours < 12 && hours != 0)
{
mil = hours + 12;
}
else if (hours = 12 && (type == 'A' || type == 'a')) {
mil = 0;
}
else {
mil = 12;
}
cout << "\The time converted to 24 hour format is: " << mil << ":" << minutes;
}

}

void output24(int& choice, int minutes, int& hours1)
{
if (choice == 24)
{
int norStd = 0;
if (hours1 <= 12 && hours1 != 0)
{
norStd = hours1;
}
else if (hours1 > 12 && hours1 < 24)
{
norStd = hours1 - 12;
}
else if (hours1 == 0)
{
norStd = 12;
}
else
{
cout << "Incorrect number entered";
}
cout << "The time converted to 12 hour format is: " << norStd << ":" << minutes << " ";
if (hours1 < 12) {
cout << "A.M.";
}
else {
cout << "P.M.";
}
}

}
Please use code tags when posting code - what you're posted is not readable!


[code]
    // code goes here
[/code]


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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
using namespace std;

void output();
void time(int& choice);
void input(int& hours, int& minutes, char& type, int& choice, int& hours1);
void output12(int& hours, int minutes, char type, int& choice);
void output24(int& choice, int minutes, int& hours1);

int main()
{
	int hours, hours1;
	int minutes;
	char type;
	char answer;
	int choice;

	do {
		output();
		time(choice);
		input(hours, minutes, type, choice, hours1);
		output12(hours, minutes, type, choice);
		output24(choice, minutes, hours1);

		cout << "\n\nPerform another calculation? (y/n): ";
		cin >> answer;

	} while ((answer == 'Y') || (answer == 'y'));
	return 0;

}

void output()
{
	cout << "\nTime Conversion" << endl;
}

void time(int& choice)
{
	cout << "Enter the time format you want converted ";
	cout << "\nEx... 12 = 8:15 P.M to 20:15";
	cout << "\nex... 24 = 20:15 to 8:15 P.M.";
	cout << "\n(12 or 24): ";
	cin >> choice;
}

void input(int& hours, int& minutes, char& type, int& choice, int& hours1)
{
	if (choice == 12)
	{
		cout << "Enter the hours for the 12 hour time: ";
		cin >> hours;
		cout << "Enter the minutes for the 12 hour time: ";
		cin >> minutes;
		cout << "Enter the type of time (A for A.M. or P for P.M.):";
		cin >> type;
	}
	else if (choice == 24)
	{
		cout << "Enter the hours for the 24 hour time: ";
		cin >> hours1;
		cout << "Enter the minutes for the 24 hour time: ";
		cin >> minutes;
	}
	else {
		cout << "*** An error was made in your input ***" << endl;
	}
}

void output12(int& hours, int minutes, char type, int& choice)
{
	if (choice == 12)
	{
		int mil = 0;
		if (hours < 12 && hours != 0 && (type == 'A' || type == 'a'))
		{
			mil = hours;
		}
		else if (hours < 12 && hours != 0)
		{
			mil = hours + 12;
		}
		else if (hours = 12 && (type == 'A' || type == 'a')) {
			mil = 0;
		}
		else {
			mil = 12;
		}
		cout << "\The time converted to 24 hour format is: " << mil << ":" << minutes;
	}

}

void output24(int& choice, int minutes, int& hours1)
{
	if (choice == 24)
	{
		int norStd = 0;
		if (hours1 <= 12 && hours1 != 0)
		{
			norStd = hours1;
		}
		else if (hours1 > 12 && hours1 < 24)
		{
			norStd = hours1 - 12;
		}
		else if (hours1 == 0)
		{
			norStd = 12;
		}
		else
		{
			cout << "Incorrect number entered";
		}
		cout << "The time converted to 12 hour format is: " << norStd << ":" << minutes << " ";
		if (hours1 < 12) {
			cout << "A.M.";
		}
		else {
			cout << "P.M.";
		}
	}

}

Topic archived. No new replies allowed.