Hello,
Can someone explain why "inline if" does not work in this particular example:
Looks like this "inline if" is really unhappy with different variable types (string and char), but if-else statement causes no errors.Code:#include <iostream> using namespace std; string replaceStr(string source, char a, string b) { string result = ""; for(int i=0; i<source.length(); i++) { char ch = source[i]; // I have no idea why this doesn't work: // result += (ch == a) ? b : ch; if(ch == a) result += b; else result += ch; } return result; } int main() { string s = "banana"; cout << s << endl; // outputs: banana s = replaceStr(s, 'a', "--"); cout << s << endl; // outputs: b--n--n-- return 0; }



2Likes
LinkBack URL
About LinkBacks



