Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Comparison with single '='?

Reply
Thread Tools

Comparison with single '='?

 
 
Johs
Guest
Posts: n/a
 
      04-19-2007
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.
 
Reply With Quote
 
 
 
 
Michael Ekstrand
Guest
Posts: n/a
 
      04-19-2007
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
 
Reply With Quote
 
 
 
 
Marco Wahl
Guest
Posts: n/a
 
      04-19-2007
On Apr 19, 2:58 pm, Johs <(E-Mail Removed)> wrote:
> In Pratical C++ Programming by Steve Oualline (O'Reilly) I found this
> code example:
>
> #include <iostream.h>


http://www.parashift.com/c++-faq-lit....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

 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      04-19-2007
On Apr 19, 8:58 am, Johs <(E-Mail Removed)> 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.


 
Reply With Quote
 
Old Wolf
Guest
Posts: n/a
 
      04-22-2007
On Apr 20, 12:58 am, Johs <(E-Mail Removed)> 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.

> }


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-23-2007
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.

 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      04-23-2007
In article <(E-Mail Removed) .com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) says...
> On Apr 20, 12:58 am, Johs <(E-Mail Removed)> 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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-23-2007
On Apr 23, 12:23 am, Old Wolf <(E-Mail Removed)> wrote:
> On Apr 20, 12:58 am, Johs <(E-Mail Removed)> 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. 199,
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:(E-Mail Removed)
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

 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      04-23-2007
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
Custom Taglib problems - instead of a single instance per page, I have a single instance per application. chris brat Java 1 05-10-2006 11:16 AM
Asp.NET Javascript string, want to pass '(single quotes' within '(single quotes) Chris ASP .Net 1 03-24-2006 09:03 PM
BoundColumn single DataBind equiv but not in a single row? Randall Parker ASP .Net 1 12-12-2005 04:11 PM



Advertisments