c++字符数组提取子串

用字符数组的方法怎么做?什么情况下会输出-1?

img


#include <iostream>
#include <string>

int main()
{

    int length = 0;
    int findNum = 0;
    std::string str;
    std::cin >> length;
    int tmp = 0;
    getchar();
    getline(std::cin, str);
    std::string::size_type position;
    while (tmp < length)
    {
        std::string tmpstr;
        tmpstr.clear();
        for (int i = tmp; i < length; i++)
        {
            tmpstr.append(&str[i]);
            for (int j = i + 1; j < length; j++)
            {
                position = str.find(tmpstr);
                if (position != str.npos)
                {
                    // 找到了
                    findNum++;
                }

            }
        }
        tmp++;
    }

    if (findNum == 0)
    {
        std::cout << "-1";
    }
    else
    {
        std::cout << findNum;
    }
    system("pause");
    return 0;
}