Issue using for()

So I am working on some homework for class. We are to use for() to complete this but I am having issues getting it to work. We are to display every combination of military time then exit the program. so 00:00:00, 00:00:01, 00:00:02 and so forth until 23:59:59. I think I have decent base going (plan to tidy up the output after I get the function working right) but visual studios isn't counting right. Giving my limited knowledge of for() what am I doing wrong? Code is below. Any help is appreciated.

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
//Write a program to print all combinations of hours, minutes and seconds in military time format HH:MM:SS.
//You may display each combination on a new line.  Hint -  use nested loops.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void Pause();

int main()
{
bool done;
int h, m, s;

done = (h >= 25);

while (!done)
{
	for ( int h = 0, h <= 24, ++h )
	{
	cout << h << ":" << m << ":" << s << endl;
	
		for ( int m = 0, m < 60, ++m )
		{
		cout << h << ":" << m << ":" << s << endl;
		
			for ( int s = 0, s < 60, ++s )
			{
			cout << h << ":" << m << ":" << s << endl;
			}
		}
	}
done = (h >= 25);
}

Pause();
return (0);
}

void Pause() 
{ string junk; 
  cin.ignore();
  cout << "Press Enter to continue ... "; 
  getline(cin, junk);
}
Last edited on
but visual studios isn't counting right.

What does this really mean, computers are very very good at counting?

What is the program outputting?

What exactly do you expect?


for ( int h = 0, h <= 24, ++h )

Be careful, you're probably going too far. Remember you start at 00:00:00 and should probably stop at 23:59:59.

Last edited on
Ok so first off there is no need for done boolean and the logic that goes with it, sorry it's serving no good purpose. Next, for in your conditions for your for loops you use commas (,) when you should be using, or rather have to use semi-colons(;). Next, in your for loops, you have the right idea, good job! The only mistake in your for loop logic is in the first for loop you have h<=24 but you want to end at the end of 23 so you should just use h<24. In your for loops you have it output more times than needed to see below for simple logic version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
	for (int h = 0; h < 24; ++h)
	{
		for (int m = 0; m < 60; ++m)
		{
			for (int s = 0; s < 60; ++s)
			{
				cout << h << ":" << m << ":" << s << endl;
			}
		}
	}

	return (0);
}
Thank you joe86864, that solved it. Not sure why I was using bool, it just felt good?! :D True computers are good at counting but in this instance I was not very good at instructing it on how to count. It was just outputing 1:0:0, 1:1:0, 1:1:1, and counting up in that pattern. I can get the exact later for you but joe has solved this for me. True the <= 24 was to high of a number to get the output I was in need of.
Topic archived. No new replies allowed.