another nested for loop snag

Hey guys. I put together some code that finds a word in a string (string x called strx) and returns a substring around that word (string y called range1). I am now running another for loop on string y to find a second word. The word is in string y, but the for loop isn't executed and "no match was found here" is returned. (I'm accessing one hashTable called "happy" in string x and one called "unhappy" in string y. The word "angry" is in string y and in hashTable "unhappy" and should be found.) Hopefully this is a minor syntax issue:

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
using namespace std;

int main()
{
  int limit = 1;
  HashNode hashTable;
  const std::string strx = " Pissed off and how much angry? No giggling up a storm allowed!";

  const std::string happy = "(?:\\w+\\W+){4}giggling(?:\\W+\\w+){4}";

  const std::regex re(happy, std::regex::icase);

  std::smatch match;
  bool confirmWord = false;
  if (std::regex_search(strx, match, re))
  {
    std::string range1 = match[0].str();
    std::cout << range1 << '\n';

    for (int i = 0; i < hashTable.unhappy.keyWord.size(); i++)
    {
      std::string check = hashTable.unhappy.keyWord.at(i);
      int count = 0;
      int j = 0;

      for (int j = 0; j < range1.length(); j++)
      {
        if (!(range1.length() <= check.length()))
        {
          break;
          // std::cout << "Break." << endl;
        }
        if (range1[j] == check[j])
          count++;

        if (count == check.length())
        {
          std::cout << "Found " << range1 << " in list unhappy " << 
          hashTable.unhappy.keyWord.at(i) << endl;
          limit++;
          confirmWord = true;
          break;
        }
      };
      if (confirmWord)
      {
        break;
      }
      else
      {
        std::cout << "no match was found here\n";
      }
    };
  }
  else
  {
    std::cout << "no match was found\n";
  }
}
Last edited on
Please edit your post to put [code][/code] tags around the code.
Use the <> icon on the right.
There we go.
There's no x or y in your code.
Your hashTable has no entries to begin with.

> std::string check = hashTable.unhappy.keyWord.at(i);
So output 'check' as a debug string, or use a debugger.

> if (range1[j] == check[j])
Seriously!?
You're comparing two strings one character at a time?

http://www.cplusplus.com/reference/string/string/substr/
If you want to make sure one is an initial substring of another.
It was just part of the explanation. "string x" refers to the variable strx. "string y" refers to the variable range1.

I am running it in the console. Everything works except the nested for loop. I nested working loops and now the nested one is not called ever. Your link is the tutorial I used already.

Regardless of checking one character at a time, why isn't the inner for loop run?
Post something we can copy/paste/compile/run.

If we have to guess a bunch of pre-conditions, then it just won't happen.
The above should be treated like a main.cpp file without its imports. The hash table is not relevant to why the inner for loop is not called. I can't post the actual program. This is an example of a syntax issue.

There are no preconditions. It simply brings in a word from a list in the hash table to search 'strx' for and then makes a range around it called range1, then searches range1.

There is some syntactic reason the inner for loop won't search range1 created around the word "giggling".

range1 is (-4 words, giggling, +4 words).
range1 is searched for the word "angry" (this should happen and doesn't)

This prints out in the console:

./a.out
how much angry? No giggling up a storm allowed
no match was found
> The hash table is not relevant to why the inner for loop is not called.

Huh?
for (int i = 0; i < hashTable.unhappy.keyWord.size(); i++)

Are you in some other universe where 0 < 0 is true?
My point is I am running it on my end using the hashTable file. I need someone to read the code and point out what is stopping the loop. I'll look into why the hash table wouldn't be getting imported, but nothing is set up differently than the working version. Do you see anything wrong with the code provided it isn't an issue accessing the hash table?
This is debugging 101.

You put a breakpoint on line 20 and run the code.

You then single step the code to the next instruction.

If you wind up at line 50-something, then you need to examine how you set up your hashTable.

If you get to line 22, then step again and then examine the 'check' variable to see if it contains a string you expect. You might be reading the file OK, but the result can still be a hash table full of garbage.

You can figure out a lot from just using the debugger to step through the code and look what happens. Whenever you have those "I didn't expect..." moments, then there's a good change you found a bug.

> Do you see anything wrong with the code provided it isn't an issue accessing the hash table?
No.
You're a 100% right. I extracted the code for testing and it caused hash table connection errors. I wrote everything into the program that I reviewed and it compiled the first time. I got a vector error when running it that I'll be wrapping my head around, but with debugging we found which loop it is. Thanks for the guidance.
Topic archived. No new replies allowed.