Array and If Statement - Can't Get Expected Output

I'm incredibly new to C++, in my first semester of a university computer science course.

I've been trying to write code for an assignment where we're supposed to have the user input 24 hour time and then do things with it from there, but at the moment I've only been trying to have it output the times that were input and I can't for the life of me work out why I'm getting the input I've been getting.

My program so far is looking like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
using namespace std;

int main()
{
	int HH[1], MM[1], D, i;
	for( i = 0; i < 2; i += 1 )
	{
		cout << "Please enter the " << i+1 << "st time in 24 hour time (e.g. 2010, 0800):\n";
		cin >> HH[i] >> MM[i];
		if( HH[i] > 23 )
		{
			cout << "Please enter a valid time for hours.\n";
		}
		else if( MM[i] > 59 )
		{
			cout << "Please enter a valid time for minutes.\n";
		}
	}
	cout << HH[0] << MM[0] << " and " << HH[1] << MM [1] << endl;
	

	return 0;
}


At the moment, there still needs to be white space between the HH[i] and the MM[i] variables for it to read it in the way I want it to, but even when white space is left the output I'm getting is looking more like "HH[0]HH[1] and HH[1]MM[1]+1", so if I were to input 10 57 and 12 42, the output I'm getting is "1012 and 1243".

That output is all I need help with right now. Thank you for your time.
It's actually not very complex; in fact, you're already doing what you want with the text " and " between the numbers. Just output a space (or perhaps a colon) between the HHs and the MMs in the same way.

Hope this explains it enough without telling you exactly what to type, but post again if you still can't figure it out.
But why is it adding one to the second MM and ignoring the first one completely?

For example, if I'm inputting: HH[0] = 12, MM[0] = 50, HH[1] = 10, MM[1] = 40, then the output I am expecting is "1250 and 1040", however the output I'm actually getting is "1210 and 1041" and I have no idea why.
I don't know why you're getting the output that you are, but I have to warn you that your arrays are only size 1, capable of only storing a single value. You are trying to input 2 values into your array.
Oh OK, I was under the impression that they could store 2 values as 0 was counted, but I guess that's just a Basic thing. I'll change that and see if it fixes it.
Oh, I apologize. I missed your actual question when I answered.

fg109 is correct, and this undefined behavior of accessing past the end of your array may be what is resulting in the strange output.

I'm not certain exactly what you mean by "zero is counted", but I think the following will explain things:
When you declare an array, you must input its size:
int HH[1]; // array of size 1
This contains one element, which you can access with HH[0].
Similarly:
int HH[2]; // array of size 2
This contains two elements, which you can access with HH[0] and HH[1], respectively.
Topic archived. No new replies allowed.