Can't simulate a simple "clock"

Hello, I'm trying to simulate a clock using only while statements.. I used this code in Python but when I do it in Borland C++ it's not very effective.. I'm not sure what I'm doing wrong but I'm not very familiar with how exactly the while statement works in C++ compared to other languages.. what could be wrong in this code?

As an extra separate question.. is there a way to make it go second by second in real time like an actual clock?

Thanks.

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
#include<iostream>
int main()
{
int hr = 0, min = 0, sec = 0, con = 0;

cout<<"Insert hours:"<<endl;
cin>>hr;
cout<<"Insert minutes:"<<endl;
cin>>min;
cout<<"Insert seconds:"<<endl;
cin>>sec;

while(con == 0)
   {
   while(hr <= 24);
      {
      while(min < 60);
         {
         while(sec < 60);
            {
            cout<<hr<<":"<<min<<":"<<sec<<endl;
            sec = sec + 1;
            }
         min = min + 1;
         sec = 0;
         }
      hr = hr + 1;
      min = 0;
      }
   hr == 0;
   }

}
I'm a beginner too so I apologize in advance that my input might not be optimal or enough to fix your error, but I do believe you need to add

using namespace std;

under your #include for the cout and endl statements to be identifiable

And the variables can probably be stored outside of the main function (I don't think it matters though)

edit; I see you are using MIN as a variable name, I am not sure but it might be a keyword in C++ so you need to call the variable something different.

edit2: This works :) (excuse my lack of imagination for the new variable name, just changed it quickly)
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
#include <iostream>

using namespace std;

int hr = 0, minee = 0, sec = 0, con = 0;

int main()
{

	cout << "Insert hours:" << endl;
	cin >> hr;
	cout << "Insert mineeutes:" << endl;
	cin >> minee;
	cout << "Insert seconds:" << endl;
	cin >> sec;

	while (con == 0)
	{
		while (hr <= 24);
		{
			while (minee < 60);
			{
				while (sec < 60);
				{
					cout << hr << ":" << minee << ":" << sec << endl;
					sec = sec + 1;
				}
				minee = minee + 1;
				sec = 0;
			}
			hr = hr + 1;
			minee = 0;
		}
		hr == 0;
	}

}


If you are using Visual Studio IDE you will have to add #include "stdafx.h" before you include the iostream for it to compile (but I just read you aren't so don't mind).
Last edited on
In addition to the missing using namespace std...

You have semicolons after some of your while statements. This is legal, but it has the effect of making the loop do nothing until the condition is met. And since there's nothing that changes the variables in those conditions... either your loops won't run, or they'll run forever.

Also, line 30 uses the wrong equals and line 15 should probably be a <.

Finally, I'm going to strongly recommend that you ditch Borland C++. That bit of software is a fossil, and C++ has changed considerably since the Embarcadero toolchain was last called "Borland C++".
https://isocpp.org/get-started

You can have it go (roughly) second by second by adding in a one-second delay, but it's probably better to cross that bridge once the program works otherwise.

@Cannor
Welcome! Just a heads up, having global variables is something we generally try to avoid promoting here. Those variables can be global, but shouldn't be.

-Albatross
Last edited on
closed account (E0p9LyTq)
C++ provides access to several clocks in the <chrono> header, system clock, steady clock and high resolution clock:

http://www.cplusplus.com/reference/chrono/

The Boost library provides Timer classes that might be better suited for creating a clock.

http://www.boost.org/doc/libs/1_61_0/libs/timer/doc/index.html
Topic archived. No new replies allowed.