variable and loop problem(short code)

Hi! I have this kind of problem: I implement the global variable " currentTime = k * dt;" . Then i use this variable in function and don't know why currentTime=0 in each step. I checked the value of k*dt and currnetTime in each step. How is it possible that in each step I have different values of k*dt and currnetTime?

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include<iostream>
#include<cmath>
#include <fstream>

using namespace std;

#define PI       3.14159265358979323846

#define DimX 100	//number of X nodes 
#define DimY 60	//number of Y nodes
#define Dimt 100000 //time step

typedef long double real;


int k;

real t = 24*3600;  //the time of simulation [s] 

real Tair = 0; //The Temperature outside

real dt = t / Dimt;
real currentTime = k * dt;





void test_tmepretaure_function()
{
	/*
	cout << "the time of simulation is " << endl;
	cout << t / (24 * 3600) << " day(s)" << endl;
	cout << t / 3600 << " hour(s) " << endl;
	cout << t / 60 << " minute(s)" << endl;
	cout << t << " s " << endl;
	*/
	cout << "current time= " << currentTime << "[s]" << endl;

	for (k = 0; k < Dimt; k++)
	{
		
		cout.precision(10);

		if (k < (Dimt / 2))
		{
			cout << "t= " << t << endl;
			cout << "dt " << dt << endl;
			cout << "k= " << k << endl;
			
			cout << "k*dt " <<k*dt<< endl;
			cout << "current time= " << currentTime << "[s]" << endl;

			Tair = (273 + 15)*(sin(PI*(currentTime / t)))+(273-15);
			
			cout << "Tair= " << Tair - 273 << " [C] " << endl;
			//system("cls");
			system("pause");
		}
		if (k >= (Dimt / 2))
		{
			Tair = 273;
			cout << "current time= " << currentTime << "[s]" << endl;
			cout << "Tair= " << Tair - 273 << " [C] " << endl;
			system("cls");
		}
	}

}

int main()
{

	test_tmepretaure_function();
	system("pause");
}
A very common beginners mistake, is to assume that an assignment is analogous to writing an equation, i.e. making a relationship between variables. An equation, at least in the mathematical sense, describes a numerical balance, where either side of the equation is affected by the other.

An assignment, however, is not an equation. You initialize (assign) 'currentTime' the value of k*dt once on line 23 when it is declared, and then never change it again.
Topic archived. No new replies allowed.