Comparison with single '='?

Discussion in 'C++' started by Johs, Apr 19, 2007.

  1. Johs

    Johs Guest

    In Pratical C++ Programming by Steve Oualline (O'Reilly) I found this
    code example:

    #include <iostream.h>
    int result; // the result of the calculations
    char oper_char; // the user-specified operator
    int value; // value specified after the operator
    int main()
    {
    result = 0; // initialize the result
    // Loop forever (or till we hit the break statement)
    while (1) {
    cout << "Result: " << result << '\n';
    cout << "Enter operator and number: ";
    cin >> oper_char;
    cin >> value;
    if (oper_char = '+')
    result += value;
    } else {
    cout << "Unknown operator " << oper_char << '\n';
    }
    }
    return (0);
    }

    In the if statement only one '=' is used:

    if (oper_char = '+')

    Should that not be '==' or is there some exception in C++ where you can
    compare chars with only '='?

    Later in the text they type 'if (oper_char = '+')' again so it seems
    that its not a typo - unless the O'Reilly author has been in a rush.
     
    Johs, Apr 19, 2007
    #1
    1. Advertising

  2. On Thu, 19 Apr 2007 14:58:51 +0200, Johs wrote:
    > In Pratical C++ Programming by Steve Oualline (O'Reilly) I found this
    > code example:
    >
    > #include <iostream.h>
    > int result; // the result of the calculations
    > char oper_char; // the user-specified operator
    > int value; // value specified after the operator
    > int main()
    > {
    > result = 0; // initialize the result
    > // Loop forever (or till we hit the break statement)
    > while (1) {
    > cout << "Result: " << result << '\n';
    > cout << "Enter operator and number: ";
    > cin >> oper_char;
    > cin >> value;
    > if (oper_char = '+')
    > result += value;
    > } else {
    > cout << "Unknown operator " << oper_char << '\n';
    > }
    > }
    > return (0);
    > }
    >
    > In the if statement only one '=' is used:
    >
    > if (oper_char = '+')
    >
    > Should that not be '==' or is there some exception in C++ where you can
    > compare chars with only '='?
    >
    > Later in the text they type 'if (oper_char = '+')' again so it seems
    > that its not a typo - unless the O'Reilly author has been in a rush.


    Not having read the book myself, I would say it is almost certainly a
    typo. '=' is assignment, never comparison (theoretically, a class could
    overload '=' to mean comparison, but this is a Really Bad Thing and is not
    done here).

    There are times to use an '=' in the expression of an if or while
    statement, but it's essentially useless to do so when the right-hand side
    is a constant.

    - Michael
     
    Michael Ekstrand, Apr 19, 2007
    #2
    1. Advertising

  3. Johs

    Marco Wahl Guest

    On Apr 19, 2:58 pm, Johs <> wrote:
    > In Pratical C++ Programming by Steve Oualline (O'Reilly) I found this
    > code example:
    >
    > #include <iostream.h>


    http://www.parashift.com/c -faq-lite/coding-standards.html#faq-27.4

    > int result; // the result of the calculations
    > char oper_char; // the user-specified operator
    > int value; // value specified after the operator
    > int main()
    > {
    > result = 0; // initialize the result
    > // Loop forever (or till we hit the break statement)
    > while (1) {
    > cout << "Result: " << result << '\n';
    > cout << "Enter operator and number: ";
    > cin >> oper_char;
    > cin >> value;
    > if (oper_char = '+')


    Looks like a "{" is missing here.

    > result += value;
    > } else {
    > cout << "Unknown operator " << oper_char << '\n';
    > }
    > }
    > return (0);
    >
    > }
    >
    > In the if statement only one '=' is used:
    >
    > if (oper_char = '+')
    >
    > Should that not be '==' or is there some exception in C++ where you can
    > compare chars with only '='?


    Yes this should be '=='.

    Using '=' here means to have always the same condition
    in the if statement. Which in turn means that the
    if statement is omissible.


    HTH
     
    Marco Wahl, Apr 19, 2007
    #3
  4. Johs

    Salt_Peter Guest

    On Apr 19, 8:58 am, Johs <> wrote:
    > In Pratical C++ Programming by Steve Oualline (O'Reilly) I found this
    > code example:
    >
    > #include <iostream.h>


    #include <iostream>

    > int result; // the result of the calculations
    > char oper_char; // the user-specified operator
    > int value; // value specified after the operator
    > int main()
    > {
    > result = 0; // initialize the result
    > // Loop forever (or till we hit the break statement)
    > while (1) {
    > cout << "Result: " << result << '\n';
    > cout << "Enter operator and number: ";
    > cin >> oper_char;
    > cin >> value;
    > if (oper_char = '+')
    > result += value;
    > } else {
    > cout << "Unknown operator " << oper_char << '\n';
    > }
    > }
    > return (0);
    >
    > }
    >
    > In the if statement only one '=' is used:
    >
    > if (oper_char = '+')
    >
    > Should that not be '==' or is there some exception in C++ where you can
    > compare chars with only '='?
    >
    > Later in the text they type 'if (oper_char = '+')' again so it seems
    > that its not a typo - unless the O'Reilly author has been in a rush.


    That needs to be == as already mentioned.
    There is a way to insure that the correct operator is employed:

    if( 'x' == oper_char )
    {
    ...
    }

    Looks silly (at first) but effective.
     
    Salt_Peter, Apr 19, 2007
    #4
  5. Johs

    Old Wolf Guest

    On Apr 20, 12:58 am, Johs <> wrote:
    > In Pratical C++ Programming by Steve Oualline (O'Reilly) I found this
    > code example:


    This is a dreadful code example. Assuming you have typed it out
    correctly, the book should be called "Practically Useless C++
    Programming". Almost every single line is wrong.

    > #include <iostream.h>


    No such standard header, and never has been. You can get the
    intended effect by putting the following two lines:
    #include <iostream>
    using namespace std;

    > int result; // the result of the calculations


    It's called "result" and it will have the result? I never would have
    guessed!

    > char oper_char; // the user-specified operator
    > int value; // value specified after the operator


    More worse-than-useless commentary.

    > int main()
    > {
    > result = 0; // initialize the result


    'result' has already been initialized to 0 (by virtue of being
    declared outside
    of any function). If the author insists on some policy of always
    writing an
    explicit initializer, then the line should be:

    int result = 0;

    and it should either be outside the function or inside it (preferably
    the latter)
    but not both.

    > // Loop forever (or till we hit the break statement)


    There is no break statement.

    > while (1) {
    > cout << "Result: " << result << '\n';
    > cout << "Enter operator and number: ";


    Because a '\n' is not sent to the stream, this text may not appear
    yet.

    > cin >> oper_char;
    > cin >> value;


    There is no test for failure here.

    > if (oper_char = '+')


    This sets oper_char to be '+', as you noted, and always succeeds.

    > result += value;


    Causes undefined behaviour if there was an error inputting 'value', as
    'value' has not yet been assigned a proper value.

    > } else {
    > cout << "Unknown operator " << oper_char << '\n';
    > }
    > }


    This loop has no possible exit condition.
    Further, if the user presses the end-of-file character the program
    will
    output stuff indefinitely without waiting for any further input.

    > return (0);


    These brackets are superfluous.

    > }
     
    Old Wolf, Apr 22, 2007
    #5
  6. Johs

    Rolf Magnus Guest

    Old Wolf wrote:

    >> int main()
    >> {
    >> result = 0; // initialize the result

    >
    > 'result' has already been initialized to 0 (by virtue of being
    > declared outside of any function).


    Also, technically, this isn't initialization, but assignment.

    >> while (1) {
    >> cout << "Result: " << result << '\n';
    >> cout << "Enter operator and number: ";

    >
    > Because a '\n' is not sent to the stream, this text may not appear
    > yet.


    That's actually not true. cin and cout are tied together, which means that
    whenever you read from cin, cout gets automatically flushed.
    Also, a '\n' might or might not flush the stream, depending on its buffering
    scheme. That's why there is std::endl, which sends a '\n' _and_ flushes the
    stream.

    >> cin >> oper_char;
    >> cin >> value;

    >
    > There is no test for failure here.


    Right. If the user enters a wrong character, the program will stay in its
    endless loop, but ignore all further input.

    >> if (oper_char = '+')

    >
    > This sets oper_char to be '+', as you noted, and always succeeds.
    >> result += value;

    >
    > Causes undefined behaviour if there was an error inputting 'value', as
    > 'value' has not yet been assigned a proper value.


    But 'value' is a global variable. If the error happens during the first loop
    iteration, it should be zero, otherwise it should contain the value of the
    previous iteration. So I'd say no undefined behavior.
     
    Rolf Magnus, Apr 23, 2007
    #6
  7. Johs

    Jerry Coffin Guest

    In article <>,
    says...
    > On Apr 20, 12:58 am, Johs <> wrote:
    > > In Pratical C++ Programming by Steve Oualline (O'Reilly) I found this
    > > code example:

    >
    > This is a dreadful code example. Assuming you have typed it out
    > correctly, the book should be called "Practically Useless C++
    > Programming". Almost every single line is wrong.
    >
    > > #include <iostream.h>

    >
    > No such standard header, and never has been. You can get the
    > intended effect by putting the following two lines:
    > #include <iostream>
    > using namespace std;


    The OP doesn't seem to specify, but _if_ he's looking at the first
    edition of the book, we should probably cut it a break on this
    particular one -- the first edition seems to carry a 1995 copyright
    date, and at that time the now-standard headers were still quite new.

    [ ... ]

    > > while (1) {


    I would have added that the canonical "loop forever" is 'for (;;)' --
    though I'd also mention that I rarely find this construct useful. With
    some thought and care, the (normal) exit condition for a loop can nearly
    always be put where it belongs.

    > > cout << "Result: " << result << '\n';
    > > cout << "Enter operator and number: ";

    >
    > Because a '\n' is not sent to the stream, this text may not appear
    > yet.
    >
    > > cin >> oper_char;


    According to $27.3.1/2, cin is tied to cout by default, so at this
    point, the output above must be displayed. IOW, the program is perfectly
    fine in this particular respect.

    [ ... ]

    > > return (0);

    >
    > These brackets are superfluous.


    These specific brackets are normally called parentheses.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Apr 23, 2007
    #7
  8. Johs

    James Kanze Guest

    On Apr 23, 12:23 am, Old Wolf <> wrote:
    > On Apr 20, 12:58 am, Johs <> wrote:


    > > #include <iostream.h>


    > No such standard header, and never has been.


    There was when I started using C++. It was, in fact, the de
    facto standard up until the promulagation of C++98 (Sept. 1998),
    and even some time afterwards, until all of the libraries got
    updated. So depending on the date of the book... (Some of the
    best and most essential C++ books I have use it, e.g. Barton and
    Nackman.)

    > You can get the
    > intended effect by putting the following two lines:
    > #include <iostream>
    > using namespace std;


    That might be sufficient with most implementations, for what
    he's doing, but the effect is considerably different. The
    standard iostream are NOT compatible with the classic iostream;
    there are many subtle differences. And of course, while most
    implementations do accept it, as an extension, <iostream>
    doesn't declare any of the << operators.

    [...]
    > > while (1) {
    > > cout << "Result: " << result << '\n';
    > > cout << "Enter operator and number: ";


    > Because a '\n' is not sent to the stream, this text may not appear
    > yet.


    Are you sure? C++ doesn't have the concept of a line
    synchronized buffer, so the presence or absence of a '\n' is
    irrelevant. On the other hand, IIRC, cout is tied to cin, which
    means that any input on cin (like that in the following line)
    will cause cout to be flushed. (Note that this can cause
    reading from cin to be significantly slower than reading from an
    ifstream. In non interactive programs, it may be worth
    resetting the tie.)

    (For the rest, I agree with your evaluation. If the code is
    really verbatim out of the book, then it really should be
    avoided. Which is rather surprising for O'Reilly.)

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 23, 2007
    #8
  9. Johs

    Pete Becker Guest

    James Kanze wrote:
    > And of course, while most
    > implementations do accept it, as an extension, <iostream>
    > doesn't declare any of the << operators.
    >


    As of last week, it does. Well, for implementations of C++0x.

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
     
    Pete Becker, Apr 23, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Randall Parker
    Replies:
    1
    Views:
    625
    S. Justin Gengo
    Dec 12, 2005
  2. Chris
    Replies:
    1
    Views:
    13,756
    Oisin
    Mar 24, 2006
  3. chris brat
    Replies:
    1
    Views:
    667
    chris brat
    May 10, 2006
  4. Eric Layman
    Replies:
    3
    Views:
    665
    Rad [Visual C# MVP]
    Apr 14, 2007
  5. Deepu
    Replies:
    1
    Views:
    266
    ccc31807
    Feb 7, 2011
Loading...

Share This Page