Q:

The following expression fails to compile due to operator precedence. Using Table 4.12 (p. 166), explain why it fails. How would you fix it

0

The following expression fails to compile due to operator precedence. Using Table 4.12 (p. 166), explain why it fails. How would you fix it?

string s = "word";
string pl = s + s[s.size() - 1] == 's' ? "" : "s" ;

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <string>
using std::string;

int main() {
  string s = "word";
  //string pl = s + s[s.size() - 1] == 's' ? "" : "s";
  string pl = s + (s[s.size() - 1] == 's' ? "" : "s");

  // The precedence of the conditional operator is lower than arithmetic operator.

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now