A while loop messes up my array without being executed... why?

Hello, I am trying to create some kind of parser for MathML. It takes a piece of text code and translates it from a more intuitive code to MathML in HTML.

I have set up the code the following way:
words[] is an array that holds every word in the original text(ex: a + dog, words[0] = "a", words[1] = "-", words[2] = "dog")
respectiveWords[] is an array that holds the processed HTML code. (ex: if the word "Omega" is detected in words[1], then respectiveWords[1] will be Ω - the HTML code for omega)
append[] is array of strings (ex: at the end of the program, when writing everything to file and after everything is processed, append[2] will be written after words[2]. The output will be practically respectiveWordw[0] + append[0] + respectiveWords[1] + append[1] +...

Now, I have the following piece of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
for(int i = 0; i<nWords; i++){
...
        if(eq(words[i], "#")){
            change(respectiveWords[i], "<msub><mi></mi><mi>");
            if(eq(words[i+1], "(")){}
            else if(eq(words[i+1],"fraction")){
                cout<<"\nDETECTED FUNCTION - ANIHILATING\n";
                int currentLevel = paranthesisLevel+1;
                int j = i+2;
                bool flag = true;
//HERE IS THE "WHILE" PROBLEM
                while(flag == true){
                    if(eq(words[j], "(")){currentLevel++;}
                    else if(eq(words[j], ")")){
                        if(currentLevel == paranthesisLevel+1){
                            flag = false;
                            strcat(append[j], "</mi></msub>");
                            }
                        else{currentLevel--;}}
                   j++;}
                }
            else {strcat(append[i+1], "</mi><msub>");}}
...
bool eq(char* a, char* b) returns true if the 2 strings are the same (strcmp)


The problem is the following: that while, whether or not it is called, messes up the whole respectiveWords array, in that it makes every word of it "ÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿÿ...".
Now, if I change "while" to "if" it miraculously works and doesn't change anything.
Anyone knows what the problem might be?

EDIT: "The program messes things up even if it doesn't reach the while. For example, I never have a "#" in my code, so it never gets inside that if statement. Yet something in that while still ruins my array. If I just replace that "while" with "if", it works. How can that happen if it doesn't get inside the loop?"

EDIT2: Solved. I can't believe I'm retarded enough to completely not notice the "else" statement. Took me 3 days to see that line of code, god... the "append" array was initialized wrong.
Last edited on
Is there a good reason to use arrays and char pointers rather than vectors and strings?

Just casually skimming over the code I see several places where illegal accesses might happen.
Line 5: words[i + 1] is not necessarily a valid element.
Lines 13, 14, 15: words[j] (where j >= i + 2) is not necessarily a valid element.

If at line 10 words[i + 2] == "(" and words[i + 3] == ")", the while on line 12 will never terminate. There's probably other ways to make it misbehave.
@helios the only reason is because I'm more used to the default things in C, but I should really learn to use those.

I am though aware of the issues you pointed out but the thing is, the program messes things up even if it doesn't reach the while. For example, I never have a "#" in my code, so it never gets inside that if statement. Yet something in that while still ruins my array. If I just replace that "while" with "if", it works. How can that happen if it doesn't get inside the loop?
I can't answer that question without being able to run it.
Registered users can post here. Sign in or register to post.