Please help trace through the logic of for loops!

I found myself stuck in some code I'm writing and could use another set of eyes on it. I commented what should be happening within the code and I really hope you can point out where I'm going wrong.

What I'm trying to do:

1. create a for loop that compares a each index value of an array (guess) to a phrase that I have already removed all of the white space from (newPhrase) (e.g., my word is love == mywordislove).

2. If there is a match with the current index location, I add one to a match that serves as a flag to a do while loop in my main program. NOTE: for the sake of testing, I have commented out anything that doesn't relate to this function.

3. If there isn't a match, I enter another for loop. There is a temp variable (t) that grabs the value of i (i.e., the current index value of newPhrase) which serves to not alter the real index of newPhrase in the outer loop. The value of guess' index never changes since I'm comparing that specific index for at least one occurrence of the guess[j]'s value in the entire newPhrase array.

4. If there is at least one match, it breaks the loop and returns to the outer loop of the function. If there isn't any match at all, then the index value (guess[j]'s value) doesn't exist and the the user has entered false information.

5. If they entered false information, then the match variable (controls the loop in main) gets a negative value which will terminate the loop in main that allows the user to guess 5 times.

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

//compares the word to the contents of the string and ensures that its members
//are valid and contained within it
void validGuess(char guess[], char newPhrase[], int & score)
{
   int j = 0;
   int match = 0;

   int len = 0;

   len = strlen(newPhrase);

   for(int i = 0; i < len; ++i)
      {
          // the purpose of this loop is to see if the element guessed exists anywhere in the newPhrase
          if(guess[j] != newPhrase[i])
              {
                 int tempMatch = 0;

                 // loops through the entire newPhrase[i] and compares it to guess[j]
                 // if an element is found in guess that doesn't match the newPhrase[i] character
                 // it enters the inner for loop where another comparison happens
                 // a temp local variable t is created for the specific variable index in question
                 // if there is a match it will break the inner loop and the outer loop
                 // will contiue to compare the values of the other elements of the guess
                 // if the index value of guess[j] doesn't have any matches in the array
                 // the match value is set to a < 0 value which then will quit the do while loop in main.
               
                 for(int t = i; t < len; ++t)
                    {
                        if(guess[j] == newPhrase[t])
                            {
                                tempMatch += 1;
                                break; //sorry, the only way it works
                            }
                    }

                   if(tempMatch == 0)
                    {
                         cout << "\nInvalid guess, one of your elements doesn't exist in the phrase." << endl;
                       match = -100; //to make sure the if condition is reached and the loop in main ends
                        break; //sorry, the only way it works
                    }

              }


         //checks if there is any match at all 
         if (guess[j] == newPhrase[i])
                    match += 1;

          ++j;

     }
         //if there is an invalid guess, this will quit the loop in main
        if(match <= 0)
           {
               cout << "Not a valid word" << endl;
               cout << "You lose your turn " << endl;
               score = -10;
           }



}

Last edited on
Hey there, sorry you're having a rough time, can you tell us what is happening that you don't expect to happen?

Without context or a verbose definition it's a bit hard to see unintended code in this single function.

Can you help me out with context?
Thanks for the response. I'll remove the topic because I realize I was doing this entirely wrong from the beginning. I need to compare words, not individual elements. Sorry for the wasted time!

Thank you for all responses
No wasted time here, glad you figured it out!
Topic archived. No new replies allowed.