3 questions from a beginner

Hi, I'm new to C language but I started to learn it recently and I'm writing a complex program that has to perform a time-simulation that does something like this:

- First of all a "logic module" verifies several public variables and chooses an "action" to perform.
- These "actions" are classes that perform operations with the same public variables that are used by a logic module mentioned before
- Each action gets logged.

The simplified idea is to make something 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
25
26
27
28
int reduce(int a, int b) {    //action 1, reduces control variable by 10
int tmp;
tmp = a-10;
cout<<"Action 'Reduce',second "<< b <<", new value of variable is "<< tmp<<endl;
return tmp;
}
int increase(int a, int b) { //action 2, increases control variable by 7
int tmp;
tmp = a+7;
cout<<"Action 'Increase',second "<< b <<", new value of variable is "<< tmp<<endl;
return tmp;
}

int _tmain(int argc, _TCHAR* argv[])
{   
int counter = 0;
int finish = 20; // time of simulation in seconds
int a = 45,n;

for (n=counter; n<finish; n++) {  //"logic" module
	if (a>=50) 
		a=reduce(a,n);
	else
		a=increase(a,n);
}
cout<<"Simulation over at second "<<n;
	return 0;
}


If you get the idea, then I'll explain you the problems I face when writing this program. The reduce() and increase() events described above are just examples. My real needs are getting events like these to work:

-increment value of a variable by 15 during 20 seconds, substracting these 15 again after "time" expires

or even worse:

-increment value of a variable each 3 seconds by 8, ending by a second 12, giving 5 "ticks" in total (numbers are just examples).

all of the events described (permanent increase, permanent reduce, temporary increase and "ticking" increase) should coexist together on the same "timeline", operating with the same variables, even several instances of events may be summoned at a time (referring to the timed ones).

Well... the questions are:

1. What kind of "time motor" should I choose? I guess that "for (t=start, t<finish, t++) is not the best choice, remember that I need some events to occur several seconds after calling a funcion, and I don't see way to do it.
2. cout logging will not make it to me... I need to store the data in some useful format like XML or on a SQL server I have to parse it afterwards, can you suggest something on that?
3. I feel that the best way to do this is by calling class-like structures that can coexist in as many copies as I need and can execute code on being summoned and destroyed but I still don't see the way to get a "ticking" class. Any suggestions on any of my questions are appreciated.

Bob.
Topic archived. No new replies allowed.