Timer (class and function)

i'm a beginner to programming and got an assignment to create a stopwatch/timer, that counts in seconds/minutes/days etc
Does anybody know a way to create a timer through classes, functions and loops without using the clock function or whatever?

So far I managed to create a class for seconds and a bit of a function but it still doesn't count and when i try add the loop the build crashes (see below) and when i try put it in the function it also crashes. any help would be great, thanks

#include <iostream>
using namespace std;

class stopwatch
int seconds;
int minutes;
int hours;
int days;
int months;
int years;
seconds = 0;
minutes = 0;
hours = 0;
days = 0;
months = 0;
years = 0;

int main()
for(int i=1, i<60, i++)
cout << i << endl;
return 0;
without using the clock function or whatever?

It's impossible. You need to use some kind of gettick or clock function. There is no way to tell how much time has passed otherwise.

Simply spinning in a loop will not tell you anything, because it's subject to how fast the computer is and how much cpu time your program is getting. Not to mention it consumes (wastes) lots of cpu time to do that.
Last edited on
i thought maybe you could do a delay like with a sendmidimessage but i realised that its not an independent command.

i dont understand why we'd be set an assignment on something we haven't seen before!
I find it extremely hard to believe that your teacher expects you to write a stopwatch class without using an underlying timer function. It really is impossible... seriously.

Are you sure you're understanding the assignment correctly?
thats what i thought too, thanks for clearing it up.
oh and the assignment is to 'encapsulate time' inside of a class, it being a stopwatch was the only thing i could think of that made sense!
can anyone help me with this as its got to be in friday and i currently haven't even got anything that builds?
Topic archived. No new replies allowed.