Using goto within if statement

I want to write a bit of code with an operation within another. The piece I have looks 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#include <stdio.h> 
#include <string.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <sstream>
using namespace std;
#define PI 3.14159265358979323846

int main()
{

  int a,b,c,d,f,i,j,k,m,n,s;
  double p[3],FM[3],theta,phi,Time,Averagetime,Energy,energy,Distance,Length,DotProdForce,Forcemagnitude,
         ForceMagnitude[201],Force[201][4],E[1000001],En[501],Epsilon[4],
         x[201][4],new_x[201][4],y[201][4];

  clock_t t1,t2;
  t1=clock();

n=2;

while(n<3){

  b=1;
  Time=0.0;
  
  while(b<11){

    clock_t t3,t4;
    t3=clock();

    /*  set the number of points */

    /* check that there are no more than 100 points */
    if(n>200){
      cout << n << " is too many points for me :-( \n";
      exit(0);
    }
  
    /* reset the random number generator */
    srand((unsigned)time(0));  
  
    for (i=1;i<=n;i++){
      x[i][1]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
      x[i][2]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
      x[i][3]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
    
      Length=sqrt(pow(x[i][1],2)+pow(x[i][2],2)+pow(x[i][3],2));
    
      for (k=1;k<=3;k++){
        x[i][k]=x[i][k]/Length;
      }
    }

    /* calculate the energy */
    Energy=0.0;
  
    for(i=1;i<=n;i++){
      for(j=i+1;j<=n;j++){
        Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
		            +pow(x[i][3]-x[j][3],2));
      
        Energy=Energy+1.0/Distance;
      }
    }
  
    /* Save Original Points */
    for(i=1;i<=n;i++){
      y[i][1]=x[i][1];
      y[i][2]=x[i][2];
      y[i][3]=x[i][3];
    }
  
    /* Loop for random points m times*/
    m=10;
  
    if (m>100){
      cout << "The m="<< m << " loop is inefficient...lessen m \n";
      exit(0);
    }
  
    a=1;
  
    while(a<m){

      /* assign random points */
      for (i=1;i<=n;i++){
        x[i][1]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
        x[i][2]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
        x[i][3]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
        
        Length=sqrt(pow(x[i][1],2)+pow(x[i][2],2)+pow(x[i][3],2));
    
        for (k=1;k<=3;k++){
          x[i][k]=x[i][k]/Length;
        }
      }

      /* calculate the energy */
      energy=0.0;
  
      for(i=1;i<=n;i++){
        for(j=i+1;j<=n;j++){
          Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
		                +pow(x[i][3]-x[j][3],2));
        
          energy=energy+1.0/Distance;
        }
      }
    
      if(energy<Energy)
        for(i=1;i<=n;i++){
          for(j=1;j<=3;j++){
            Energy=energy;
            y[i][j]=x[i][j];
          }
        }
      else
        for(i=1;i<=n;i++){
          for(j=1;j<=3;j++){
            energy=Energy;
            x[i][j]=y[i][j];
          }
        }
      
      a=a+1;
    }
  
    /* Create string stream 1 */
    ostringstream String1;
    String1 << "Bestrandonpoints_" << n;  //add number

    /* Output these points */
    ofstream File1 (String1.str().c_str());
    File1 << "Energy=" << Energy << "\n";
    for(i=1;i<=n;i++){
      File1 << x[i][1] << " " <<   x[i][2] << " " << x[i][3] << "\n";
    }
    File1.close(); 

    /* For energy file later */
    E[0]=Energy;

    /* Start doing gradient flow approach */
    a=1;
    s=0;
    f=0;
    Forcemagnitude=1.0;

    p[a]=0.1;

loop:

    while(Forcemagnitude>0.0001 && a<3){

      /* Reset initial force and energy change */
  
      for(i=1;i<=n;i++){ 
        Force[i][1]=0.0; 
        Force[i][2]=0.0; 
        Force[i][3]=0.0; 
      } 
      /* Calculate force on each particle */

      for(i=1;i<=n;i++){ 
        for(j=1;j<i;j++){
        
          Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
                        +pow(x[i][3]-x[j][3],2));
                        
          Force[i][1]=Force[i][1]+((x[i][1]-x[j][1])/(pow((Distance),3))); 
          Force[i][2]=Force[i][2]+((x[i][2]-x[j][2])/(pow((Distance),3))); 
          Force[i][3]=Force[i][3]+((x[i][3]-x[j][3])/(pow((Distance),3))); 
        } 

        for (j=i+1;j<=n;j++){ 

          Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
                        +pow(x[i][3]-x[j][3],2));
                        
          Force[i][1]=Force[i][1]+((x[i][1]-x[j][1])/(pow((Distance),3))); 
          Force[i][2]=Force[i][2]+((x[i][2]-x[j][2])/(pow((Distance),3))); 
          Force[i][3]=Force[i][3]+((x[i][3]-x[j][3])/(pow((Distance),3)));
        }
      }

      /* Add the force to my points */
      for(i=1;i<=n;i++){

        DotProdForce=Force[i][1]*x[i][1]+Force[i][2]*x[i][2]+Force[i][3]*x[i][3];
      
        y[i][1]=x[i][1];
        y[i][2]=x[i][2];
        y[i][3]=x[i][3];
    
        Force[i][1]=Force[i][1]-DotProdForce*y[i][1];
        Force[i][2]=Force[i][2]-DotProdForce*y[i][2];
        Force[i][3]=Force[i][3]-DotProdForce*y[i][3];

        x[i][1] = y[i][1]+p[a]*(Force[i][1]);
        x[i][2] = y[i][2]+p[a]*(Force[i][2]);
        x[i][3] = y[i][3]+p[a]*(Force[i][3]);

        /* Bring it back onto sphere */
      
        Length=sqrt(pow(x[i][1],2)+pow(x[i][2],2)+pow(x[i][3],2));

        for (j=1;j<=3;j++){
          x[i][j]=x[i][j]/Length;
        }
      }
      
      /* Calculate the new energy */
    
      energy=0.0;
  
      for(i=1;i<=n;i++){
        for(j=i+1;j<=n;j++){
      
          Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
                        +pow(x[i][3]-x[j][3],2));
          energy=energy+1.0/Distance;
        }
      }

      E[a]=energy;

      /* Choose best energy and therefore best points */
      if (energy<Energy)
        Energy=energy,s=s+1;
      else
        energy=Energy,f=f+1,p[a]=0.95*p[a];
 
      for(i=1;i<=n;i++){
        ForceMagnitude[i]=pow((pow(Force[i][1],2)+pow(Force[i][2],2)
                               +pow(Force[i][3],2)),0.5);
      }

      for(i=1;i<=n-1;i++){
        if(ForceMagnitude[i]<ForceMagnitude[i+1])
          ForceMagnitude[i]=ForceMagnitude[i+1];
        else
          ForceMagnitude[i+1]=ForceMagnitude[i];
      }

      Forcemagnitude=ForceMagnitude[n];
      
      FM[a]=Forcemagnitude;
      
      cout << "FM=" << Forcemagnitude << "\n";
 
      a=a+1;
    }
    
      if(sqrt(pow((p[1]-p[2]),2))<0.0001)
        cout << "p[1]=" << p[1] << " p[2]" << p[2] << "\n";
        break;
      else if(FM[2]<FM[1])
        for(i=1;i<=n;i++){
          for(j=1;j<=3;j++){
            x[i][j]=y[i][j];
            p[1]=1.05*p[2];
            goto loop;
          }
        }  
      else if(FM[2]>FM[1])
        for(i=1;i<=n;i++){
          for(j=1;j<=3;j++){
            x[i][j]=y[i][j];
            p[1]=0.95*p[2];
            goto loop;
          }
        }
      else
        break;

    En[b]=Energy;
    
    b=b+1;
  
    t4=clock();
    float diff ((float)t4-(float)t3);
    float seconds = diff / CLOCKS_PER_SEC;
  
    Time = Time + seconds;
    
  }

  Averagetime = Time/(b-1);

  t2=clock();
    float diff ((float)t2-(float)t1);
    float seconds = diff / CLOCKS_PER_SEC;
    
  n=n+1;
  }
  
  return 0;

}


I am trying to find the largest value for p for which FM decreases. The value of FM depends on the points x, and so with a change in n the largest value of p for FM to decrease changes. I basically want to check if FM gets bigger or smaller, if smaller increase p and start again. If bigger decrease p and start again. What is the largest value of p for which FM will decrease?? Using goto within the if statement (259) can't be done, how would I write this if statement another way?


Any help appreciated, thanks, A.
Create a boolean to determine whether or not you want to break out of the structure or carry on given that the previous block was left.
I have decided on a much simpler approach of just while loop for certain number of times as follows:

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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include <stdio.h> 
#include <string.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <sstream>
using namespace std;

int main()
{

  int a,b,c,d,f,i,j,k,m,n,s;
  double p[3],FM[3],theta,phi,Time,Averagetime,Energy,energy,Distance,Length,DotProdForce,Forcemagnitude,
         ForceMagnitude[201],Force[201][4],E[1000001],En[501],Epsilon[4],
         x[201][4],new_x[201][4],y[201][4];

n=2;

while(n<3){

  b=1;
  Time=0.0;
  
  while(b<11){

    /*  set the number of points */

    /* check that there are no more than 100 points */
    if(n>200){
      cout << n << " is too many points for me :-( \n";
      exit(0);
    }
  
    /* reset the random number generator */
    srand((unsigned)time(0));  
  
    for (i=1;i<=n;i++){
      x[i][1]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
      x[i][2]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
      x[i][3]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
    
      Length=sqrt(pow(x[i][1],2)+pow(x[i][2],2)+pow(x[i][3],2));
    
      for (k=1;k<=3;k++){
        x[i][k]=x[i][k]/Length;
      }
    }

    /* calculate the energy */
    Energy=0.0;
  
    for(i=1;i<=n;i++){
      for(j=i+1;j<=n;j++){
        Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
		            +pow(x[i][3]-x[j][3],2));
      
        Energy=Energy+1.0/Distance;
      }
    }
  
    /* Save Original Points */
    for(i=1;i<=n;i++){
      y[i][1]=x[i][1];
      y[i][2]=x[i][2];
      y[i][3]=x[i][3];
    }
  
    /* Loop for random points m times*/
    m=10;
  
    if (m>100){
      cout << "The m="<< m << " loop is inefficient...lessen m \n";
      exit(0);
    }
  
    a=1;
  
    while(a<m){

      /* assign random points */
      for (i=1;i<=n;i++){
        x[i][1]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
        x[i][2]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
        x[i][3]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
        
        Length=sqrt(pow(x[i][1],2)+pow(x[i][2],2)+pow(x[i][3],2));
    
        for (k=1;k<=3;k++){
          x[i][k]=x[i][k]/Length;
        }
      }

      /* calculate the energy */
      energy=0.0;
  
      for(i=1;i<=n;i++){
        for(j=i+1;j<=n;j++){
          Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
		                +pow(x[i][3]-x[j][3],2));
        
          energy=energy+1.0/Distance;
        }
      }
    
      if(energy<Energy)
        for(i=1;i<=n;i++){
          for(j=1;j<=3;j++){
            Energy=energy;
            y[i][j]=x[i][j];
          }
        }
      else
        for(i=1;i<=n;i++){
          for(j=1;j<=3;j++){
            energy=Energy;
            x[i][j]=y[i][j];
          }
        }
      
      a=a+1;
    }
  
    /* Create string stream 1 */
    ostringstream String1;
    String1 << "Bestrandonpoints_" << n;  //add number

    /* Output these points */
    ofstream File1 (String1.str().c_str());
    File1 << "Energy=" << Energy << "\n";
    for(i=1;i<=n;i++){
      File1 << x[i][1] << " " <<   x[i][2] << " " << x[i][3] << "\n";
    }
    File1.close(); 

    /* For energy file later */
    E[0]=Energy;

    /* Start doing gradient flow approach */
    a=1;
    s=0;
    f=0;
    d=1;
    Forcemagnitude=1.0;

    p[a]=0.1;


    while(d<500)
    while(Forcemagnitude>0.0001 && a<3){

      /* Reset initial force and energy change */
  
      for(i=1;i<=n;i++){ 
        Force[i][1]=0.0; 
        Force[i][2]=0.0; 
        Force[i][3]=0.0; 
      } 
      /* Calculate force on each particle */

      for(i=1;i<=n;i++){ 
        for(j=1;j<i;j++){
        
          Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
                        +pow(x[i][3]-x[j][3],2));
                        
          Force[i][1]=Force[i][1]+((x[i][1]-x[j][1])/(pow((Distance),3))); 
          Force[i][2]=Force[i][2]+((x[i][2]-x[j][2])/(pow((Distance),3))); 
          Force[i][3]=Force[i][3]+((x[i][3]-x[j][3])/(pow((Distance),3))); 
        } 

        for (j=i+1;j<=n;j++){ 

          Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
                        +pow(x[i][3]-x[j][3],2));
                        
          Force[i][1]=Force[i][1]+((x[i][1]-x[j][1])/(pow((Distance),3))); 
          Force[i][2]=Force[i][2]+((x[i][2]-x[j][2])/(pow((Distance),3))); 
          Force[i][3]=Force[i][3]+((x[i][3]-x[j][3])/(pow((Distance),3)));
        }
      }

      /* Add the force to my points */
      for(i=1;i<=n;i++){

        DotProdForce=Force[i][1]*x[i][1]+Force[i][2]*x[i][2]+Force[i][3]*x[i][3];
      
        y[i][1]=x[i][1];
        y[i][2]=x[i][2];
        y[i][3]=x[i][3];
    
        Force[i][1]=Force[i][1]-DotProdForce*y[i][1];
        Force[i][2]=Force[i][2]-DotProdForce*y[i][2];
        Force[i][3]=Force[i][3]-DotProdForce*y[i][3];

        x[i][1] = y[i][1]+p[a]*(Force[i][1]);
        x[i][2] = y[i][2]+p[a]*(Force[i][2]);
        x[i][3] = y[i][3]+p[a]*(Force[i][3]);

        /* Bring it back onto sphere */
      
        Length=sqrt(pow(x[i][1],2)+pow(x[i][2],2)+pow(x[i][3],2));

        for (j=1;j<=3;j++){
          x[i][j]=x[i][j]/Length;
        }
      }
      
      /* Calculate the new energy */
    
      energy=0.0;
  
      for(i=1;i<=n;i++){
        for(j=i+1;j<=n;j++){
      
          Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
                        +pow(x[i][3]-x[j][3],2));
          energy=energy+1.0/Distance;
        }
      }

      E[a]=energy;

      /* Choose best energy and therefore best points */
      if (energy<Energy)
        Energy=energy,s=s+1;
      else
        energy=Energy,f=f+1,p[a]=0.95*p[a];
 
      for(i=1;i<=n;i++){
        ForceMagnitude[i]=pow((pow(Force[i][1],2)+pow(Force[i][2],2)
                               +pow(Force[i][3],2)),0.5);
      }

      for(i=1;i<=n-1;i++){
        if(ForceMagnitude[i]<ForceMagnitude[i+1])
          ForceMagnitude[i]=ForceMagnitude[i+1];
        else
          ForceMagnitude[i+1]=ForceMagnitude[i];
      }

      Forcemagnitude=ForceMagnitude[n];
      
      FM[a]=Forcemagnitude;
      
      cout << "FM=" << Forcemagnitude << "\n";
 
      a=a+1;
    }
    
      if(sqrt(pow((p[1]-p[2]),2))<0.0001)
        cout << "p[1]=" << p[1] << " p[2]" << p[2] << "\n";
      else if(FM[2]<FM[1])
        for(i=1;i<=n;i++){
          for(j=1;j<=3;j++){
            x[i][j]=y[i][j];
            p[1]=1.05*p[2];
            d=d+1;
          }
        }  
      else if(FM[2]>FM[1])
        for(i=1;i<=n;i++){
          for(j=1;j<=3;j++){
            x[i][j]=y[i][j];
            p[1]=0.95*p[2];
            d=d+1;
          }
        }
      else
        d=d+1;
      
    }  

    En[b]=Energy;
    
    b=b+1;

  }
  
  n=n+1;
  
  }
  
return 0;

}


However the following errors occur:

expected unqualified-id before ‘return’
expected declaration before ‘}’ token

Any ideas as to why??

Thanks, A.
You have an extra } in your program somewhere. The } on line terminates main() causing a parsing error on return on line 286.
Yep I have found it thanks...now just an infinite loop to sort out...
IMO, you have far too much code in main - 288 lines is way too much. There is lots of opportunity to split this into functions - as your comments may indicate what could be a function.

This will make it much easier to deal with.
I second TheIdeaMan's comment about too much code in main.

That is the reason I could not identify which { } pair was mismatched.
Had your code been broken into discrete functions, it would have been much easier to identify where the unmatched { } occurred.

Breaking your code into smaller function will also help in identifying where your infinite loop is. tl;dr really appies here to your main. Really would like to help here, but main is really too complex for a casual observer to identify your problem.



I appreciate that the code is crude. I haven't been coding very long, and not very intensely either. The idea of using functions makes sense, but I haven't learnt how to do it yet and just wanted something quick to help with answering a question I have for another piece of code (finding a good p value). I don't have time before exams to really get stuck into C, and by then this question shall be irrelevant.

I shall carry on coding after but more for me than for answering maths questions. Then I shall learn how to code properly and so you won't see this kind of simple rubbish happening, but until then, I must proceed with the knowledge I have as I just don't have enough time.

Thanks for the help so far guys and the constructive criticism! I shall try to find the loop later tonight but need to work on my dissertation a bit for now!

A
Also, with braces & parentheses etc - My editor automatically does closing braces, if you are using something more simple, then get into the habit of typing the closing brace straight after the opening one, then go back and fill in the details. This applies to any thing that comes in pairs - braces, parentheses, square brackets, angle brackets, quotes or C style comment.

That way you should never have a problem with mismatches.

Good luck with your dissertation & exams - hope all is well :)
TextWrangler has little open and closes on the side so easy to find the bracket, I just didn't know what the error message meant. Does this error always mean a bracket issue??

Good luck with your dissertation & exams - hope all is well :)


Thanks, I hope so too!
Does this error always mean a bracket issue??


Most likely.

If you use the typing strategy I mentioned you will never have this problem again.

The clang compiler has really great easy to understand compiler messages - whereas all the others are a bit cryptic until you get used to them - an example of the more mistakes you make, the more you learn !

Being able to use clang depends on how you compile - from the cmd line no problem. But if you use an IDE, it has to be of the type that allows you to select which compiler to use. Microsoft is notable in that you can't.

I use KDevelop or QtCreator on Linux. KDevelop lets me compile all kinds of languages & scripts, not just C++. And it is all 100% free, Linux has the potential to download 20,000 free apps as well, and is very secure - no more virus protection programs.
Topic archived. No new replies allowed.