Issues with do-while loop and values not updating

I am writing a program to simulate a GPS system. I hard-coded in the data so I could debug in Visual Studio. I will actually be running the program in a terminal so I can pipe in data. The problem is in the do-while loop. I am recalculating 3 variables until it satisfies the condition. The second time through, tnew does not change. It does the first time, so the only reason I can come up with that it doesn't is that the change is so small that it's insignificant. I am almost positive this is the problem. Any help would be great, I've been debugging this for hours on end:

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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include<iostream>
#include<fstream>
#include<sstream>
#include<string>
#include<iomanip>
#include<math.h>
#include<cmath>
using namespace std;

int main()
{
	// Read in data.dat into a temporary vector
	string line;
	ifstream myfile;
	int index = 0;
	float a, temp[220];

	myfile.open("data.dat");
	if (!myfile)
		cout << "Unable to open file!" << endl;

	while (!myfile.eof())
	{
		while (getline(myfile, line))
		{
			istringstream iss(line);
			iss >> skipws >> a;
			temp[index] = a;
			index++;
		}
	}
	myfile.close();

	// Assign values from temporary vector to actual variables
	double pi = temp[0];
	double c = temp[1];
	double R = temp[2];
	double s = temp[3];
	double u[24][3]; // double indexed u vector
	double v[24][3]; // double indexed v vector
	double per[24][1]; // periodicity vector
	double alt[24][1]; // altitude vector
	double ph[24][1]; // phase vector

					  // Assign the values for the satellites into double indexed vectors u and v
					  // Read in satellite data using loops
	int i = 4;
	for (int j = 0; j < 24; j++)
	{
		for (int k = 0; k < 3; k++)
		{
			u[j][k] = temp[i];
			i++;
		}
		for (int l = 0; l < 3; l++)
		{
			v[j][l] = temp[i];
			i++;
		}
		for (int m = 0; m < 1; m++)
		{
			per[j][m] = temp[i];
			i++;
		}
		for (int n = 0; n < 1; n++)
		{
			alt[j][n] = temp[i];
			i++;
		}
		for (int p = 0; p < 1; p++)
		{
			ph[j][p] = temp[i];
			i++;
		}
	}

	//Now read in data output by  vehicle
	string vehicleInput;
	ofstream outFile;
	outFile.open("mySatellite.log");
	int is = 0;
	//while (getline(cin, vehicleInput))
	//{
		double tv, latd, latm, lats, NS, longd, longm, longs, EW, h;
		//istringstream vehicleInfo(vehicleInput);
		//vehicleInfo >> tv >> latd >> latm >> lats >> NS >> longd >> longm >> longs >> EW >> h;
		tv = 12123.0;
		latd = 40;
		latm = 45;
		lats = 55;
		NS = 1;
		longd = 111;
		longm = 50;
		longs = 58;
		EW = -1;
		h = 1372;

		double latitude = 2.0 * pi * NS * (latd / 360.0 + latm / (360.0 * 60.0) + lats / (360.0 * 60.0 * 60.0));
		double longitude = 2.0 * pi * EW * (longd / 360.0 + longm / (360.0 * 60.0) + longs / (360.0 * 60.0 * 60.0));
		double tempx = (R + h)*cos(latitude)*cos(longitude);
		double tempy = (R + h)*cos(latitude)*sin(longitude);
		double tempz = (R + h)*sin(latitude);;
		double alpha = (2.0 * pi*tv) / s;

		// Multiply the vector x y z by the rotation matrix
		// This is hard-coded in
		double x = tempx*cos(alpha) - tempy*sin(alpha);
		double y = tempx*sin(alpha) + tempy*cos(alpha);
		double z = tempz;
		double xv[3] = { x,y,z };

		// Go through each satellite and calculate ts and xs
		for (int is = 0; is<24; is++)
		{
			// Compute ts and xs
			long double Initialxs[3];
			for (int kk = 0; kk<3; kk++)
			{
				Initialxs[kk] = (R + h)*(u[is][kk] * cos(((2.0 * pi*tv) / (s / 2.0)) + ph[is][0]) + v[is][kk] * sin(((2.0 * pi*tv) / (s / 2.0)) + ph[is][0]));
			}
			long double told = tv;
			long double tnew = tv - (sqrt((Initialxs[0] - xv[0])*(Initialxs[0] - xv[0]) + (Initialxs[1] - xv[1])*(Initialxs[1] - xv[1]) + (Initialxs[2] - xv[2])*(Initialxs[2] - xv[2]))) / c;
			long double check = abs(tnew - told);
			long double xs[3];
			long double dummy;
			do {
				told = tnew;
				for (int ii = 0; ii < 3; ii++)
				{
					xs[ii] = (R + h)*(u[is][ii] * cos(2 * pi*told / (s / 2) + ph[is][0]) + v[is][ii] * sin(2 * pi*told / (s / 2) + ph[is][0]));
				}
				dummy = (sqrt(pow((xs[0] - xv[0]), 2.0) + pow((xs[1] - xv[1]), 2.0) + pow((xs[2] - xv[2]), 2.0))) / c;
				tnew = tv - (sqrt((xs[0] - xv[0])*(xs[0] - xv[0]) + (xs[1] - xv[1])*(xs[1] - xv[1]) + (xs[2] - xv[2])*(xs[2] - xv[2]))) / c;
				check = abs(tnew - told);
				cout << check << endl;
			} while (check > (0.01 / c));
			
			
			long double ts = tnew;
			
			// Output all data to a file
			outFile << is << "  " << setprecision(20) << ts << "  " << setprecision(20) << xs[0] << "  " << setprecision(20) << xs[1] << "  " << setprecision(20) << xs[2] << endl;


			// Check to see if the satellite is above the horizon and if it is, output the data
			double xvTs = x*xs[0] + y*xs[1] + z*xs[2];
			double xvTxv = pow(x, 2) + pow(y, 2) + pow(z, 2);
			if (xvTs>xvTxv)
			{
				// Output data
				cout << is << "  " << setprecision(20) << ts << "  " << setprecision(20) << xs[0] << "  " << setprecision(20) << xs[1] << "  " << setprecision(20) << xs[2] << endl;
			}
		}
	//}
		system("pause");
	return 0;
}
Last edited on
This is useless. Post the full code. What the hell is this even doing? Why are you naming an interator 'kk'. Also put your code in code blocks or people on here won't even bother looking at your code. You can do that by highlighting all the code and then hitting the <> button to the right. Also the reason why tnew doesn't change has to do with your while condition. check is probably > (0.01/c). If I were you I would output the value that is stored in check to see what's going on.
Last edited on
I have so many for loops that I wanted to differentiate between them which is why I have 'kk.' I was outputting check, but it is always 0 the second time around because tnew isn't changing. I'm super new to coding so go easy on me :)
Topic archived. No new replies allowed.