As the title states, I am attempting to write a program that searches a sentence for a word and then outputs the location of that word, which I will do in the main function. The actual testing for the word must be a function separate from main.
#include <iostream>
#include <string>
using namespace std;
int searchWord(string sentence, string test);
int main()
{
string sentence;
string test;
cout << "Please enter a sentence." << endl;
getline (cin, sentence);
cout << "What word would you like to find?" << endl;
cin >> test;
searchWord(sentence, test);
return 0;
}
int searchWord(string sentence, string test)
{
int found = -1;
size_t location;
location = sentence.find(test);
if (location == sentence)
{
found = 0;
}
return found;
}
I get the following error:
'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'size_t'
1> c:\program files\microsoft visual studio 8\vc\include\string(91) : see declaration of 'std::operator =='
1>c:\users\jennifer\desktop\final\morrisjfinalproject.cpp(40) : error C2784: 'bool std::operator ==(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const _Elem *' from 'size_t'
1> c:\program files\microsoft visual studio 8\vc\include\string(81) : see declaration of 'std::operator =='
1>c:\users\jennifer\desktop\final\morrisjfinalproject.cpp(40) : error C2784: 'bool std::operator ==(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'const std::basic_string<_Elem,_Traits,_Alloc> &' from 'size_t'
1> c:\program files\microsoft visual studio 8\vc\include\string(71) : see declaration of 'std::operator =='
1>c:\users\jennifer\desktop\final\morrisjfinalproject.cpp(40) : error C2784: 'bool std::operator ==(const std::allocator<_Ty> &,const std::allocator<_Other> &) throw()' : could not deduce template argument for 'const std::allocator<_Ty> &' from 'size_t'
1> c:\program files\microsoft visual studio 8\vc\include\xmemory(174) : see declaration of 'std::operator =='
1>c:\users\jennifer\desktop\final\morrisjfinalproject.cpp(40) : error C2784: 'bool std::operator ==(const std::istreambuf_iterator<_Elem,_Traits> &,const std::istreambuf_iterator<_Elem,_Traits> &)' : could not deduce template argument for 'const std::istreambuf_iterator<_Elem,_Traits> &' from 'size_t'
1> c:\program files\microsoft visual studio 8\vc\include\xutility(2143) : see declaration of 'std::operator =='
1>c:\users\jennifer\desktop\final\morrisjfinalproject.cpp(40) : error C2784: 'bool std::operator ==(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'size_t'
1> c:\program files\microsoft visual studio 8\vc\include\xutility(1826) : see declaration of 'std::operator =='
1>c:\users\jennifer\desktop\final\morrisjfinalproject.cpp(40) : error C2784: 'bool std::operator ==(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : could not deduce template argument for 'const std::pair<_Ty1,_Ty2> &' from 'size_t'
1> c:\program files\microsoft visual studio 8\vc\include\utility(60) : see declaration of 'std::operator =='
1>c:\users\jennifer\desktop\final\morrisjfinalproject.cpp(40) : error C2677: binary '==' : no global operator found which takes type 'std::string' (or there is no acceptable con
I know the problem is somewhere in the function. I just don't know why.
got it, thanks. Now I just have one more small problem: the statement that prints out if the word is found is printing twice(?!)
Here is the modified code:
#include <iostream>
#include <string>
using namespace std;
int searchWord(string sentence, string test);
int main()
{
string sentence;
string test;
cout << "Please enter a sentence." << endl;
getline (cin, sentence);
cout << "\nWhat word would you like to find?" << endl;
cin >> test;
searchWord(sentence, test);
if (searchWord(sentence, test) == -1)
{
cout << "\n" << test << " not found in " << sentence << endl;
}
return 0;
}
int searchWord(string sentence, string test)
{
int found = -1;
size_t location;
location = sentence.find(test);
if (location != string :: npos)
{
found = 0;
cout << "\n" << test << " found at " << int(location) << endl; \\this is printing double
}
ok so I have had to modify this program to my teachers specifications. I am getting one syntax error. It is in the line in the main where I am calling the function.
[code]/*#include <iostream>
#include <string>
using namespace std;
int wordSearch(char sentence[], char test[]);
int main()
{
char sentence[] = "The cat in the hat";
char test[] = "begin";
//char test[] = "cat";
wordSearch(sentence[], test[]);
return 0;
}
int wordSearch(char sentence[], char test[])// syntax error : ']'
{
for (int j = 0; j < NULL; j++)
{
for (int k = 0; k < NULL; k++)
{
if (sentence[j] == test[k])
{
return j;
}
}
}
I'm not sure if this is why the error is occuring, but when you are passing the arguments to wordSearch in main(), you don't put the [] on them.
Also, I think your word search is bugged, testing whether an int is <= NULL will compare it to zero, not what you want. Also, you for loops seem to be trying to loop through the entire array and check everything against everything else, which doesn't make sense.