Dont know what I'm doing wrong!! Please Help!!

I dont know what I'm missing here and why I keep getting the following warnings which cause my program to crash:

1
2
3
4
5
: warning C4305: '=' : truncation from 'int' to 'char'
: warning C4309: '=' : truncation of constant value
: warning C4305: '=' : truncation from 'int' to 'char'
: warning C4309: '=' : truncation of constant value
: warning C4700: uninitialized local variable 'display' used


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
#include <iostream>
using namespace std;

int getTime(int &hours, int &minutes);
void convertTime(int hours, int minutes);
void printTime(int hours, int minutes, char display);


int main()

{
	int hours, minutes;
	char display;
	cout << "---------------------------------------------------------" << endl;
	cout << "            24-Hour to 12-Hour Time Converter            " << endl;
	cout << "---------------------------------------------------------" << endl;

	getTime(hours, minutes);

	convertTime(hours, minutes);

	printTime(hours, minutes, display);

	return 0;

}

int getTime(int &hours, int &minutes)
{
	cout << "Please enter the hour and minutes in 24-Hour format" << endl;
	cin >> hours, minutes;
	return hours, minutes;
}

void convertTime(int hours, int minutes)
{
	char display;
	if(hours > 12)
	{
		hours = hours - 12;
		display = 'PM';
	}
	else
	{
		display = 'AM';
	}
	
}

void printTime(int hours, int minutes, char display)
{
	cout << "The time converted to a 12 hour format is: " << hours << ":" << minutes << display << endl;
}


The original instructions where this:
Write a program with three functions, one using reference parameters which asks the user to enter an hour value and a minutes value in 24 hour format, eg 22 09, another to which you pass the hour and a char variable (both by reference) which will convert it to 12 hour format. The function will set the char variable to AM or PM. Then a third function using value parameters to display the time in 12 hour format.

Any help would be appreciated!!

Thanks in advance!
I actually figured it out already on my own!


Thanks anyway :-)
I know you figured it out, but I'm looking for practice :)

Line 52 and 56 should only assign 1 character to display since they both end with an M anyway. That changes your output.

I also added if statements in the printTime function so if you have minutes less than 10 it doesn't read 12:3 AM but rather 12:03 AM.

Passing a char to functions by reference confuses me, so I just made the convertTime function return a character. (The integers in the function will still be changed because it is passed by reference.)

Also the getTime function doesn't need to be of type int, because when you pass by reference you can still edit the variables.

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
// practice.cpp : main project file.

#include "stdafx.h"

#include <iostream>
using namespace std;

void getTime(int &hours, int &minutes);
char convertTime(int& hours);
void printTime(int hours, int minutes, char display);

int main()

{
	int hours = 0;
	int minutes = 0;
	char display = ' ';
	cout << "---------------------------------------------------------" << endl;
	cout << "            24-Hour to 12-Hour Time Converter            " << endl;
	cout << "---------------------------------------------------------" << endl;

	getTime(hours, minutes);

	display = convertTime(hours);

	printTime(hours, minutes, display);
	
	return 0;

}

void getTime(int &hours, int &minutes)
{
	cout << "Please enter the hour and minutes in 24-Hour format" << endl;
input:
	cin >> hours;
	cin >> minutes;
	if (hours <= 24 && hours >=0 && minutes <= 60 && minutes >= 0)
		return;
	else 
	{
		cout << "Error! Please enter a valid time.\n";
		goto input;
	}
}

char convertTime(int& hours)
{
	if(hours > 12)
	{
		hours -= 12;
		return 'P';
	}
	else
	{
		return 'A';
	}
	
}

void printTime(int hours, int minutes, char display)
{
	if (minutes < 10)
	{
		cout << "The time converted to a 12 hour format is: "
			 << hours << ":0" << minutes << " "<< display << "M" << endl;
	}
	else
	{
		cout << "The time converted to a 12 hour format is: "
			 << hours << ":" << minutes << " "<< display << "M" << endl;
	}
}
Topic archived. No new replies allowed.