beginner here, Im getting this error within my program which is supposed to find a word in a string then, replace that word with any that you input. When I input multiple words into the str1, it says this:
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::erase:__pos (which is 18446744074709551615) > this->size() (which is 9)
Heres my code:
#include <iostream>
#include <string>
using namespace std;
void findReplace(string& str1,string& str2,string& str3);
int main()
{
string str1, str2, str3;
cout << "Enter a sentence that you would like to analyze: \n";
cin >> str1;
cin.ignore();
cout << "Enter a word that you would like to search for: \n";
cin >> str2;
cin.ignore();
cout << "Enter a word that would replace the word that was found: \n";
cin >> str3;
cin.ignore();
findReplace(str1,str2,str3);
return 0;
}
void findReplace(string& str1,string& str2,string& str3)
{
int length= 0;
int str2len= 0;
int str3Length = 0;
length = str1.length();
str2len = str2.length();
str3Length = str3.length();
int found = str1.find(str2);
if ((found!= string::npos))
{
cout << str2 << " found at " << found << endl;
}
str1.erase(found, str2len);
str1.replace(found, str3Length, str3 );
cout << str1;
}