please help me

how do i change this digital clock to car odometer in a car traveling 1 mile every second. Using nested loops and have one loop per each digit in an odometer that can represent 6 digits to the left of the decimal point and one digit to the right of the decimal point: 999999.9

//main program
int main()
{
//Data
int hr = 0; //hours (2 digits width) in digital clock
int min = 0; //minutes (2 digits width) in digital clock
int sec = 0;// seconds (2 digits width) in digital clock

// output my name and objective and program information
cout << "Objective: This program will simulate a digital clock.\n";
cout << "Programmer: Teacher\n";
cout << "Editor(s) used: Notepad\n";
cout << "Compiler(s) used: TDM MinGW\n";
cout << "File: " << __FILE__ << endl;
cout << "Complied: " << __DATE__ << " at " << __TIME__ << endl << endl;
cout << endl << endl << "CTRL-C to exit...\n";

//output digital clock
cout.fill('0'); //format the initial digital clock to 00:00:00
for (hr = 0; hr < 24; hr++)
{
for (min = 0; min < 60; min++)
{
for (sec = 0; sec < 60; sec++)
{
//output hours, minutes, seconds of digital clock
cout << setw(2) << hr << ':';
cout << setw(2) << min << ':';
cout << setw(2) << sec << ' ';
cout.flush(); //clears the output buffer

//pause for one second
#ifdef _WIN32
Sleep(1000); // one thousand milliseconds
#else
sleep(1); // one second
#endif

//return to begining of current line
cout << '\r'; // CR
} //for seconds
} // for minutes
} //for hours
}//main
You have a car travelling 1 mile every second?! Crikey, where did you get it? Where can you drive it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>
#include <thread>
#include <chrono>
using namespace std;

int main()
{
   double odo = 0.0;
   cout << setfill( '0' ) << setprecision( 1 ) << fixed;
   for ( int i = 0; i <= 9999999; i++ )
   {
      cout << setw( 8 ) << odo << endl;
      odo += 0.1;
      this_thread::sleep_for( chrono::milliseconds( 100 ) );
   }
}
Last edited on
You need to format your code so it's readable ... it's as simple as putting it around some tags:
[code]... your code here ...[/code]


Also, how do i change this digital clock to car odometer isn't a great question because it makes no sense. A digital clock gives the current TIME. A car odometer gives the DISTANCE TRAVELED by the car.

But perhaps it's some weird conversion you have to make as an exercise. In that case, can you give us some examples of what the output should be? If I understand correctly, you're representing the current time as a float where 1 second = 1.0 mile and sub-second represents fraction of a mile (e.g., 100ms = 1/10 second = (1/10)(1.0 mile) = .1 mile).

So if the current time is 14:47:55.30 the Hour/Minute/Second/Sub-second components of the odometer reading are
1
2
3
4
Hour: 14 * (3600 Sec/Hr) * (1.0 Miles/Sec) = 50400 Miles
Minute: 47 * (60 Sec/Min) * (1.0 Miles/Sec) = 2820 Miles
Second: 55 * (1.0 Miles/Sec) = 55 Miles
Sub-Second: 0.3 * (1.0 Miles/Sec) = .3 Miles


and you take the sum of each component to get the final "odometer reading":

50400 + 2820 + 55 + 0.3 = 53257.3

and since the odometer format is 6 digits wide and 1 float digit long:

53257.3 should be 053257.3

Edited:
Thank you for the correction.
Last edited on
You have a car travelling 1 mile every second?! Crikey, where did you get it? Where can you drive it?

Germany!

FYI A true high powered rifle bullet takes 2 seconds for this if it had no air resists. ( @>2600+ feet/sec, some touch 3k/sec).
Last edited on
Topic archived. No new replies allowed.