Code stops working after adding if statements.

Hi. I have a bunch of checkboxes used to write different values to a file. The checkBox1 control does what it's supposed to. But when I add another checkBox45 to the code it stops working. Any ideas?

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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
Hi. I have a bunch of checkboxes used to write different values to a file. The checkBox1 control does what it's supposed to. But when I add another checkBox to the code it stops working. Any ideas?

[code]
 int Write_Bytes(int Bytes_To_write, int N_Bytes, int location, std::string File_Path_Music)
	   {
		   ofstream fout;
		   fout.open(File_Path_Music, ios_base::out | ios_base::in | ios_base::binary);
		   fout.seekp(location); // The position in the file to write the bytes to.
		   fout.write((char*)&Bytes_To_write, N_Bytes); //  N_Bytes is the number of bytes to write.
		   fout.close();
		   return 0;
	   }

	  

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
	int location = 0;
	int N_Bytes = 1;
	int Bytes_To_write;

	if (Bytes_To_write > 255) { N_Bytes = 2; }
	else { N_Bytes = 1; }

	std::string File_Path_Music;

	try {
		
		OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
		//openFileDialog1->InitialDirectory = "c:\\";
		openFileDialog1->Filter = "Text Files|*.txt|All Files|*.*";
		openFileDialog1->FilterIndex = 1;
		openFileDialog1->RestoreDirectory = true;


		if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
		{
			using namespace msclr::interop;
			System::String^ managedString = openFileDialog1->FileName; // This creates a managed String
			File_Path_Music = marshal_as<std::string>(managedString); // This converts to a Standard string
		}

	}
	catch (...) {}



	
	// The Below changes the music pointer
///////////////////////////////////////////////////////////////////////////////////////////////// checkBox1 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	
		if (checkBox1_Song_ID == 1)
		{
			location = 0x3983e2;
				Bytes_To_write = 0x8582;
				Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 2)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8589;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 3)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x86FC;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 4) 
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8703;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);

			location = 0x398703 + 1; 
			Bytes_To_write = 0x00;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);

		}
		if (checkBox1_Song_ID == 5)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8908;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}


		if (checkBox1_Song_ID == 6)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x89F0;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}

		if (checkBox1_Song_ID == 7)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8A84;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}

		if (checkBox1_Song_ID == 8)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8B75;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 9)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8BC1;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 10)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8C16;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 11)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8C59;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 12) 
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8CE8;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 13)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8D5F;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 14)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8DC3;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		 if (checkBox1_Song_ID == 15)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8E61;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 16) 
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8E1B;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 17)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8FB4;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 18)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x9015;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 19)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x901C;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}


		// The Below Changes the index of the music. not the pointer.
		// It will change all instences of the specified music.



		if (checkBox1_Song_ID == 22)  
		{

			location = 0x398703 + 1;
			Bytes_To_write = 0x17;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}

		if (checkBox1_Song_ID == 23) 
		{
			location = 0x398703 + 1;
			Bytes_To_write = 0x14;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 24)
		{
			location = 0x398703 + 1;
			Bytes_To_write = 0x16;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 25)
		{
			location = 0x398703 + 1;
			Bytes_To_write = 0x1A;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 26)
		{
			location = 0x398703 + 1;
			Bytes_To_write = 0x11;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 27)
		{
			location = 0x398703 + 1;
			Bytes_To_write = 0x19;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 28)
		{
			location = 0x398703 + 1;
			Bytes_To_write = 0x12;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 29)
		{
			location = 0x398703 + 1;
			Bytes_To_write = 0x0C;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 30) 
		{
			location = 0x398703 + 1;
			Bytes_To_write = 0x0A;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 31) 
		{
			location = 0x398703 + 1;
			Bytes_To_write = 0x09;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		if (checkBox1_Song_ID == 32)
		{
			location = 0x398703 + 1;
			Bytes_To_write = 0x0F;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}

		if (checkBox1_Song_ID >= 22)
		{
			location = 0x3983e2;
			Bytes_To_write = 0x8703;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}
		////////////////////////////////////////////////////////////////////////////////////  checkBox45   /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// this below statement stops the code above from working


		if (checkBox2_Song_ID == 7)
		{
			location = 0x3983c2;
			Bytes_To_write = 0x8A84;
			Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
		}





 


[/code]
Last edited on
Well the first thought is that what you are writing into memory is either wrong or at the wrong address....

When you start writing data direct to memory all bets are off...

PS.Note that this is C++/cli code...
Last edited on
The addresses are correct. I tested them individually. It’s just when I add another check box to the mix things stop working.
I don't see any reason, but this is growing too big for this approach.
try making a vector of tuples with say <location, byte_address>
so you can just say write_bytes(...myvector[checkbox_ID]...) one time. That is, eliminate all the if statements and assignments entirely...

what does stop working mean, exactly?
did you verify that checkBox2_Song_ID is == 7 and step through with the debugger? what happens in the debugger for 7, anything weird, or like it skips that code block, or ???
So many if statements that could be pared down to a switch statement or two....

Lots less verbose, easier to read and understand, with a switch than a "billion" ifs.

https://www.learncpp.com/cpp-tutorial/switch-statement-basics/

jonnin's approach is a lot better, though, really. Preconfigure a container with the known/used locations/byte addresses and a single call with the song ID.

Cyclone wrote:
The addresses are correct. I tested them individually. It’s just when I add another check box to the mix things stop working.

Could it be a mistyped address for the 2nd check box?
1
2
3
4
5
6
7
8
9
10
11
12
13
	if (checkBox1_Song_ID == 7)
	{
		location       = 0x3983e2;
		Bytes_To_write = 0x8A84;
		Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
	}

	if (checkBox2_Song_ID == 7)
	{
		location       = 0x3983c2;  // <-- you sure about that address?
		Bytes_To_write = 0x8A84;
		Write_Bytes(Bytes_To_write, N_Bytes, location, File_Path_Music);
	}

A 'c' instead of an 'e' is a very easy typo to make, and hard to spot after beating one's head on a desk for a while.
That wasn’t the problem. I checked the code in a hex editor and found out that the incorrect number of bytes were being written.
Thanks for the help.
Topic archived. No new replies allowed.