skip some for loop iterations

Jan 7, 2010 at 8:36am
consider this code I made.

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
  char input[80];
  char allCloseBrace[] = {']','}','>',')'};
  char allOpenBrace[] = {'[','{','<','('};
  int i=0,j,locClose = -1,tempClose = -1,tempOpen = -1;
  bool checkOpenBrace = false;
  cout<<"Input anything with random brackets: ";
  cin<<input;
while (true){
  //for loop that searches close brace
  for(i;i<80;i++){
     for(j=0;j<4;j++){
	if(input[i] == allCloseBrace[j]){
	   locClose = i;
	   kindBrace = j;
	   break;
	}
     }
    if(locClose == 0)
       cout<<"incorrect order";//or return false to exit the while loop;
    if(locClose>0)
       break;
  }
  if(locClose == -1)
     checkOpenBrace = true;

  if(checkOpenBrace == true){
      //checks if there is/are open brace if none output correct else incorrect
  }
  tempClose = locClose;
  //for loop that searches open brace
  for(k=locClose;k>=0;k++){
      //what I want here is a code that skips tempClose and tempOpen
      for(kj = countOpen;kj>=0;kj--){
          if(storeBrace[k]==allOpenBrace[kindBrace]){
	      tempOpen = k;
	      break;
	  }
      }
  }
  i = locClose + 1;
}

for example, the user input a[s{dp[asd]asdddsd]sd{sd}<d>(s)]
the first close bracket is the close bracket at input[10].
then the first open bracket before input[10] is input[6].
since the while loop is true it will loop again.
now the first close bracket must be at input[18].
for the loop that searches open brace, it will start searching at input[17] and stop at input[11] then continue at input[5].

My question is: Is there a simplier or shorter code doing this? without using the STL for algorithm (#include <algorithm>). Thanks.


in general, what if I don't want the iterations of the for loop be executed at a certain number like
1
2
3
4
5
6
7
8
9
10
11
12
13
for(i=80;i>=0;i--){
  if(i==78)
     i =  75;
  if(i==56)
     i = 40;
  //and so on...  
}
//to be specific
for(i=locClose;i>=0;i--){
  if(i==tempClose)
    i = tempOpen - 1;
  /*where the value of locClose, tempClose, and tempOpen is changing depending on the user input*/   
}

Hmmm... in Mathematics i is a set of real numbers from 0 to 80 except for values (75,78],(40,56].
sorry I can't put that in Mathematical form.
Jan 7, 2010 at 4:51pm
This seems a rather complicated loop structure and is difficult to debug, you would be better off using a finite-state-machine, they are used in compilers or syntax recognition programs, and since they are more general you can use them for other similar tasks within your program.
Last edited on Jan 7, 2010 at 4:58pm
Jan 7, 2010 at 5:29pm
Jan 8, 2010 at 1:39am
sorry for double posting.
Topic archived. No new replies allowed.