Hello everyone, I havn't been doing C++ in awhile but I need to freshen up fast!
I'm doing the following,
I'm trying to see if somthing in string2 is in string1,
for example string2 is "eo"
String1 = "Hello"
Hello has
both an e and an o
the logic to see if "eo" in "Hello" works but once I find that yes there is an e and an o in "Hello" I want to remove the e and o, so the string remaining is
hll
Well the replace function only accepts STRINGS so I was trying to cast the character e when its found to a string, then using the string.repalce(i,1,char1);
But I keep getting that error any ideas?
The error is:Code:#include <cstdlib> #include <iostream> #include <string> using namespace std; int main() { string string1 = "Hello"; string string2 = "eo"; cout << "string1: " << string1 << endl; for(int i = 0; string1[i] != '\0'; i++) { if(string1[i] == string2[0]) { cout << "found: " << string2[0] << " in the string: " << string1 << endl; //string1[i] = string1[i+1]; string char1 = string2[0]; string1.replace(i,1,char1); } else if(string1[i] == string2[1]) { cout << "found: " << string2[1] << " in the string: " << string1 << endl; //string1[i] = string1[i + 1]; string char2 = string2[1]; string1.replace(i,1,char2); } else cout << "nothing in string2: " << string2 << "matches in string1: " << string1 << endl; } cout <<"New string: " << string1 << endl; return 0; }
1>.\test.cpp(22) : error C2440: 'initializing' : cannot convert from 'char' to 'std::basic_string<_Elem,_Traits,_Ax>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1>.\test.cpp(29) : error C2440: 'initializing' : cannot convert from 'char' to 'std::basic_string<_Elem,_Traits,_Ax>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Ax=std::allocator<char>
1> ]
1> No constructor could take the source type, or constructor overload resolution was ambiguous
1>Build log was saved at "file://c:\Documents and Settings\Mr. Coffee\My Documents\Visual Studio 2005\Projects\C program\C program\Debug\BuildLog.htm"
1>C program - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========




Reply With Quote