How do I assign a value to this?

Hi!

I'm not sure how to assign a value to the variable amOrPm (line 36).
The program converts time from 24 hours to a 12 hour clock and vice versa. However, I need to make sure that if I convert time from 24 hour to a 12 hour clock that it includes AM or PM. This is where I am stuck at!

Help would be much appreciated!
Last edited on
cout << "Enter 1 if you would like the time in AM, enter 2 if you would like time in PM. \n";

If you want them to enter 1 or 2. Why is amOrPm a char? Char is 1 character, thats all. Char can be the character b, or c, or x etc. Dont you want to use integer?
Yes, my apologies. The reason why I have it as a char is because prior to making the user enter a number, i would have the user type either AM or PM. However it didn't exactly work -- it would only output the first letter(as a char should) so I changed it.
I see. Well couldnt you just use integer now instead of char? would be easier for you.

Edit: Are you allowed to use strings? Cuz if you are, this shits easy.
Last edited on
I could, yes, however I still have the same problem -- how do I assign the value of 1 to PM and 2 to AM. It seems fairly simple however I just don't quite understand how to do it.

Just saw your edit - yes we are allowed to use strings
Last edited on
I changed it to string. But if you want to do it with integer, its practically the same. String is cool though :)

I changed all the int amOrPm to string amOrPm. Then I added this to the display function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void displayResults(int&hours, int&minutes, string& amOrPm, int& ans)//function that displays the results
{
	if (amOrPm == "1")
	{
		amOrPm = "AM";
	}
	else
	{
		amOrPm = "PM";
	}

	if (ans == 1)
		cout << "The time is: " << hours << ":" << minutes << " " << amOrPm << endl;
	else
		cout << "The time is: " << hours << ":" << minutes << endl;
}


You can see. Basically its just converting them. Just a quick though. You should be using string. And since you are here, there is no reason to use 1 or 2 for AM or PM. You can just tell the user to input AM or PM. And then you'd have to leave the display function untouched, but in the end its up to you. Im gonna post the full code with the AM or PM example I just showed.

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
void menu(int& ans);
void userInput(int& hours, int& minutes, string& amOrPm, int& ans);
void convertTimeTo24(int& hours, int& minutes);
void convertTimeTo12(int& hours, int& minutes);
void displayResults(int&hours, int&minutes, string& amOrPm, int& ans);

void menu(int& ans) //Function to display the choices
{

	cout << "************\n";
	cout << "Press 1 to convert time from 24 hour format to 12 hour format\n";
	cout << "Press 2 to convert time from 12 hour format to 24 hour format\n";
	cout << "Press 00 to terminate the program";
	cout << "\n************\n";

	cin >> ans;//gets the users choice of what they want to do in the program
}

void userInput(int& hours, int& minutes, string& amOrPm, int& ans)//Function that asks the user for input
{
	if (ans == 1)
	{
		cout << "Please enter the amount of hours: ";
		cin >> hours;
		cout << "Now enter the amount of minutes: ";
		cin >> minutes;
		cout << "Do you want it in AM or PM. \n";
		cin >> amOrPm;  // User just writes AM or PM here
	}
	else if (ans == 2)
	{
		cout << "Please enter the amount of hours: ";
		cin >> hours;
		cout << "Now enter the amount of minutes: ";
		cin >> minutes;
	}
	else if (ans == 0)
	{
		cout << "You have selected to terminate the program";
	}
}
void convertTimeTo24(int& hours, int& minutes)//converts time from 12 hour notation to 24 hour notation
{
	if (hours < 12) //hours can't be more than 24 so must make sure it's below 12 before adding 12
	{
		hours = hours + 12;
	}
	else // if hours is more than 12 no change will be made
	{
		hours = hours;
	}
}

void convertTimeTo12(int& hours, int& minutes) //converts time from 24 hour notation to 12 hour notation
{
	if (hours > 12)//makes sure hours is more than 12 since we can't have negative hours
	{
		hours = hours - 12;
	}
	else // if hours is less than 12 it will leave as is
	{
		hours = hours;
	}
}

void displayResults(int&hours, int&minutes, string& amOrPm, int& ans)//function that displays the results
{

	if (ans == 1)
		cout << "The time is: " << hours << ":" << minutes << " " << amOrPm << endl;
	else
		cout << "The time is: " << hours << ":" << minutes << endl;
}

int main()
{
	int ans, hours, minutes;
	string amOrPm;

	menu(ans);//pulls up the menu
	userInput(hours, minutes, amOrPm, ans); //pulls up the user input function
	if (ans == 1)
	{
		convertTimeTo12(hours, minutes);
	}
	else if (ans == 2)
	{
		convertTimeTo24(hours, minutes);
	}

	displayResults(hours, minutes, amOrPm, ans);

	system("pause");
	return 0;
}
Appreciate the help. I knew I was on the right track but I knew that I was definitely doing something wrong. Yeah I haven't used string in a while, usually we are just required to fix error in code and whatnot.

Thanks again
Topic archived. No new replies allowed.