-1

I'm trying to design a construct that can check for errors in input, so I wrote two boolean functions in a test program whose parameters are string references. I was hoping my if statement would evaluate the first function- changing part of word, then evaluate the second function- changing word again, and finally evaluate the && to see whether print. My code won't run, however, and the error says that no operand matches the << operator for cout, when I'm trying to output the code. My code is can explain better than I. Could someone please explain why this won't work? I'm using Visual Studio 2012.

#include <iostream>
using namespace std;

bool changeString1(string &word){
    word[0]='a';
    return word[1]=='b';
}

bool changeString2(string &word){
    word[2]='c';
    return word[3]=='d';
}

int main(){
    cout << "hello" << endl;

    string word("cbad");
    if(changeString1(word) && changeString2(word)){
        cout << word << endl;
    }
    system("Pause");
    return 0;
}
Ben
  • 5,952
  • 4
  • 33
  • 44
  • 7
    `#include ` – Brian Bi Mar 08 '14 at 01:18
  • 2
    Btw. when using `&&` right side is only evaluated if left side was true. – user2802841 Mar 08 '14 at 01:20
  • "My code won't run, however, and the error says that no operand matches the << operator for cout, when I'm trying to output the code." - you are describing a compilation error. This is different to a runtime error; your program never got up to the stage where it is ready to run. – M.M Mar 08 '14 at 02:07
  • I feel like I've grown so much since posting this question :D – Ben Apr 26 '19 at 23:27

1 Answers1

4

Your problem is probably that you haven't got #include <string> in the beginning of your code.

The order of evaulation of && and || expressions is strictly "left to right", and guaranteed to "short circuit" when the expression is determined by the left operand.

When using other operators, it is undefined, so for example if (changeString1(word) + changeString2(word)), the compiler would be allowed to call changeString2 before changeString1.

In other words, when there is an && (and) in the expression, if the first condition is false, the second expression is not evaluated, and if the first expression is true, the second is evaluated. Since both are indeed true in this case, both are evaluated.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
  • 2
    The left-to-right evaluation isn't related to the `if`; for example, in `if (foo() + bar())` the evaluation order of the two calls is unspecified. The evaluation order, and short-circuit behavior, are part of the definition of the `&&` operator, which can occur as an `if` condition or in any other context that requires an expression. – Keith Thompson Mar 08 '14 at 01:24
  • @KeithThompson: Assuming I didn't mess up my edit, I have corrected the wording to explain what I mean in a more correct manner. Of course, it doesn't have anything to do with IF, and everything to do with the operator. I was being "simple" (too simple, obviously) – Mats Petersson Mar 08 '14 at 01:30
  • Rather than "when the expression is known", I'd say "when the result of the expression is determined by the left operand". – Keith Thompson Mar 08 '14 at 02:25
  • I should have noticed wasn't there. This is a practice problem for a calculator homework for taking a string, converting it from infix to postfix, and then calculating the answer. So the line if(infixToPostfix(expr) &&postfixToAns(expr){cout << ans;} should work? I'm gonna try it. Thanks! – Ben Mar 08 '14 at 03:14
  • Yes, but I would split it up, because for error message, you probably want to do a separate "infix is broken" or "couldn't calculate answer". – Mats Petersson Mar 08 '14 at 09:21