-1

I'm using this recursive function to find the tags in a HTML, the output is okay as of now but I'm more interested in the return value of this function find_tag which comes out to be :

 -somelargenumber
  html
  head
  body

where the -somelargenumber is the returned value of function which is different every time I run the compiled program.

But according to I made the function it must return 0 at some point in time and not any other value. Can anybody explain this?

#include<string>
#include<regex>
#include<iostream>
#include<iomanip>
using namespace std;
int find_tag(const string& s,vector<string> & cont )
{
    smatch m;
    if(s.size()>0)
    {
        if(regex_search(s,m,regex("<(.*)>(.*)</\\1>")))
        {
            cont.push_back(m[1]);
            find_tag(m[2],cont);
            find_tag(m.suffix(),cont);
        }
        else
        {
            return 0;
        }
    }
    else
    {
        return 0;
    }
}
int main()
{
    cmatch m;
    vector<string> cont;
    string data {"<html><head>head data</head><body>body data</body></html>"};
    cout<<find_tag(data,cont);
    if(!(cont.empty()))
    {
        for(auto &a:cont)
        {
            cout<<a<<endl;
        }
    }
    else
    {
        cout<<"\n not found";
    }
    return 0;
}

PS: I am not trying to parse any HTML here. I am just interested in the returned value of the function.

Karup
  • 2,024
  • 3
  • 22
  • 48
rahul tyagi
  • 643
  • 1
  • 7
  • 20
  • i m more ineteresed in the recursion part why it is not returning 0 but some random number. – rahul tyagi Jul 01 '15 at 18:20
  • 6
    Your `regex_search` true branch has no `return` statement. Presumably it should return 1? – ooga Jul 01 '15 at 18:21
  • What compiler are you using? this could be related: http://stackoverflow.com/questions/12530406/is-gcc-4-7-and-gcc-4-8-buggy-about-regular-expressions – NathanOliver Jul 01 '15 at 18:24
  • 1
    Proper indenting makes it easier to see, but @ooga's statement is why you're getting an unexpected return. I've voted to close this question as a typo that is unlikely to help future visitors. – mah Jul 01 '15 at 18:27
  • i don't get any warnings – rahul tyagi Jul 01 '15 at 18:27
  • 1
    Then your warning levels aren't high or pedantic enough. – WhozCraig Jul 01 '15 at 18:28

2 Answers2

1

The answer by @hades2510 should resolve your problem.

Suggestions for improvement.

  1. Change the name of the function to find_tags since you are expecting to find many tags in a recursive manner.

  2. You don't need the return type to be int. It doesn't seem useful at all. If you have any tags, they will be returned via the output argument.

  3. If you go by that, the function can be simplified to:

    void find_tags(const string& s,vector<string> & cont )
    {
       smatch m;
       if(regex_search(s,m,regex("<(.*)>(.*)</\\1>")))
       {
          cont.push_back(m[1]);
          find_tags(m[2],cont);
          find_tags(m.suffix(),cont);
       }
    }
    
Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
0

You need to:

return find_tag(m.suffix(),cont);

Because you don't do that even though the recursion finishes you don't catch it.

The large number you're seeing is whatever the compiler finds on the stack after the recursion finishes. This depends on the compiler but most likely is a combination of the address of find_tag and some from data.

Radu Diță
  • 13,476
  • 2
  • 30
  • 34