int compare(string first, string second, int c)
{
int l = second.length();
string a = first.substr(c,l);
int n = first.length() - second.length();
if (a == second)
return count; //Error shows up here!
elseif (c > n)
return -1;
else
compare(first,second,++c);
}
I'm getting an error that says:
error: cannot resolve overloaded function `count' based on conversion to type `int'
I have looked at it over and over and can't see what's wrong... Thoughts?
Now that I got that figured out, my entire program isn't doing what I want it to do... I'm basically trying to create my own .find() function using recursion, however, I'm having issues. It works when the word I'm searching for is located in index 0, but anything else is just returning junk. Thoughts?
#include <iostream>
#include <string>
usingnamespace std;
int index_of(string, string);
int compare(string, string, int);
int main()
{
string base, second;
cout << "\nEnter in a string please(no spaces): ";
cin >> base;
cout << "\nNow enter a string to locate inside " << base << ": ";
cin >> second;
index_of(base, second);
getchar();
return 0;
}
int index_of(string f, string s)
{
int zero = 0;
int count = 0;
int comp = compare(f,s,count);
if(f==s)
cout << "\n" << s << " was found in index " << zero;
elseif (comp == -1)
cout << "\n" << s << " was not found.";
else
cout << "\n" << s << " was found in index " << comp;
}
int compare(string first, string second, int c)
{
int l = second.length();
string a = first.substr(c,l);
int n = first.length() - second.length();
if (a == second)
return c;
elseif (c > n)
return -1;
else
{
c++;
compare(first,second,c);
}
}
int compare(string first, string second, int c)
{
int l = second.length();
string a = first.substr(c,l);
int n = first.length() - second.length();
if (a == second)
return c;
elseif (c > n)
return -1;
else
{
c++;
compare(first,second,c);
}
return c;
}