The conditional statement "if(j.... always executes regardless of what
you enter.
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
char buffer[256]="";
GetWindowText(hwnd,buffer,256);
if(!strcmp(buffer,""))
{
return TRUE;
}
printf("%d. %s\n",i,buffer);
cout << "is this it?: " << flush;
int j = 0;
cin >> j;
cout << endl;
if (j=1) cout << hwnd << buffer << endl; //This always excecutes
//regardless of input
i++;
return TRUE;
}
Futher, if you change it to "int j = 1", it will never execute -
regardless of imput.
Any help would be appreciated. 8 1579
arganx wrote: The conditional statement "if(j.... always executes regardless of what you enter.
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam) { char buffer[256]=""; GetWindowText(hwnd,buffer,256); if(!strcmp(buffer,"")) { return TRUE; } printf("%d. %s\n",i,buffer);
cout << "is this it?: " << flush; int j = 0; cin >> j; cout << endl; if (j=1) cout << hwnd << buffer << endl; //This always excecutes
This is not a condition but an assignment
if( j == 1 )
Now, *this* is a condition
--
Karl Heinz Buchegger kb******@gascad.at
On 2005-11-14 16:52, Karl Heinz Buchegger wrote: This is not a condition but an assignment
if( j == 1 )
Now, *this* is a condition
Out of curiosity, can an assignment return false? And if so under which
conditions?
Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
Karl Heinz Buchegger wrote: arganx wrote:
The conditional statement "if(j.... always executes regardless of what you enter.
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam) { char buffer[256]=""; GetWindowText(hwnd,buffer,256); if(!strcmp(buffer,"")) { return TRUE; } printf("%d. %s\n",i,buffer);
cout << "is this it?: " << flush; int j = 0; cin >> j; cout << endl; if (j=1) cout << hwnd << buffer << endl; //This always excecutes
This is not a condition but an assignment
if( j == 1 )
Now, *this* is a condition
I would like to add:
If you didn't get a compiler warning for this, you should compiler with
a higher warning level. The compiler can help help you with a lot of
mistakes like this one.
Gabriel
Erik Wikström wrote: On 2005-11-14 16:52, Karl Heinz Buchegger wrote:
This is not a condition but an assignment
if( j == 1 )
Now, *this* is a condition
Out of curiosity, can an assignment return false? And if so under which conditions?
Erik Wikström
The assignment returns the content of the variable assigned to.
Thus
if (j=0)
is false and would not execute.
Gabriel wrote: Karl Heinz Buchegger wrote: arganx wrote:
The conditional statement "if(j.... always executes regardless of what you enter.
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam) { char buffer[256]=""; GetWindowText(hwnd,buffer,256); if(!strcmp(buffer,"")) { return TRUE; } printf("%d. %s\n",i,buffer);
cout << "is this it?: " << flush; int j = 0; cin >> j; cout << endl; if (j=1) cout << hwnd << buffer << endl; //This always excecutes
This is not a condition but an assignment
if( j == 1 )
Now, *this* is a condition I would like to add: If you didn't get a compiler warning for this, you should compiler with a higher warning level. The compiler can help help you with a lot of mistakes like this one.
Gabriel
Agreed. In fact, I'd suggest you should probably always compile on the
maximum warning level and alter the code (or manually disable specific
warnings) to get a warning-less build.
Also, programming style can help with cases like yours if you train
yourself to put constants first in equality tests:
if( 1 == j )
instead of
if( j == 1 )
Then there can be no confusion and compiler will always complain with
an error if you forget the second equal sign.
Cheers! --M
mlimber wrote: ... Also, programming style can help with cases like yours if you train yourself to put constants first in equality tests:
if( 1 == j )
instead of
if( j == 1 )
Then there can be no confusion and compiler will always complain with an error if you forget the second equal sign. ...
In my personal opinion (and I'm not alone in this, according to many
discussions on that subject I saw) the negative impact this habit has on
the readability of the code far outweigh the planned benefits. I, for
example, always use the "natural" order of operands in comparison
if (j == 1)
and I've never had any problems of the above nature.
The reversed notation '(1 == j)' is just one of those things that seem
to make sense in theory but have very little or no value in practice.
--
Best regards,
Andrey Tarasevich
Andrey Tarasevich wrote: mlimber wrote: ... Also, programming style can help with cases like yours if you train yourself to put constants first in equality tests:
if( 1 == j )
instead of
if( j == 1 )
Then there can be no confusion and compiler will always complain with an error if you forget the second equal sign. ...
In my personal opinion (and I'm not alone in this, according to many discussions on that subject I saw) the negative impact this habit has on the readability of the code far outweigh the planned benefits. I, for example, always use the "natural" order of operands in comparison
if (j == 1)
and I've never had any problems of the above nature.
Same here.
Those mistakes stopped long ago, when I changed my coding style from
if(j=1)
to
if( j == 1 )
Just adding whitespace (and of course getting more practice) did the trick.
--
Karl Heinz Buchegger kb******@gascad.at
>In fact, I'd suggest you should probably always compile on the maximum warning level
Usually that is very good advice, except with certain compilers (e.g.
Microsoft Visual Studio) that proceed to emit vast amounts of warnings
about the vendor supplied header files that would swamp any warnings
about your own code. So the next level down might be more helpful. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: neblackcat |
last post by:
Would anyone like to comment on the following idea?
I was just going to offer it as a new PEP until it was suggested that
I post it here for comment & consideration against PEP 308.
I'm far...
|
by: TheKeith |
last post by:
I just wrote the following script for something I'm working on:
----------------------------------------------------------------------------
-------------------
<html>
<head>
<script...
|
by: Steven T. Hatton |
last post by:
I've made no secret of the fact that I really dislike the C preprocessor in
C++. No aspect of the language has caused me more trouble. No aspect of
the language has cause more code I've read to be...
|
by: Reinhold Birkenfeld |
last post by:
Hi,
after Guido's pronouncement yesterday, in one of the next versions of Python
there will be a conditional expression with the following syntax:
X if C else Y
which is the same as today's...
|
by: Megan |
last post by:
Can you write conditional VBA code that affects only one or two
records on a continuous subform?
I have a form with a subform on it. The parent/ child field that links
the forms is CaseID. The...
|
by: Sara |
last post by:
The problem:
Conditional formatting bold, red when field Value < date() sets the
field
background to white - always - whether condition is met or not. I
want the field unfilled and just red/bold...
|
by: paulo |
last post by:
Can anyone please tell me how the C language interprets the following
code:
#include <stdio.h>
int main(void)
{
int a = 1;
int b = 10;
int x = 3;
|
by: dev_cool |
last post by:
Hello friends,
I'm a beginner in C programming. One of my friends asked me to write a
program in C.The purpose of the program is print 1 to n without any
conditional statement, loop or jump.
...
|
by: Neal Becker |
last post by:
In hindsight, I am disappointed with the choice of conditional syntax. I know it's too late to change. The problem is
y = some thing or other if x else something_else
When scanning this my...
|
by: Naresh1 |
last post by:
What is WebLogic Admin Training?
WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
|
by: Oralloy |
last post by:
Hello Folks,
I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA.
My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
|
by: Carina712 |
last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
|
by: BLUEPANDA |
last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
|
by: Rahul1995seven |
last post by:
Introduction:
In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
|
by: Ricardo de Mila |
last post by:
Dear people, good afternoon...
I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control.
Than I need to discover what...
|
by: ezappsrUS |
last post by:
Hi,
I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
|
by: jack2019x |
last post by:
hello, Is there code or static lib for hook swapchain present?
I wanna hook dxgi swapchain present for dx11 and dx9.
|
by: F22F35 |
last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...
| |