Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C++ if statements - only last one is ever true

Reply
Thread Tools

C++ if statements - only last one is ever true

 
 
root
Guest
Posts: n/a
 
      01-04-2004
Hi group,

Apologies in advance if this has been asked somewhere before, but I haven't
managed to get anything from the Google archives - I've been getting
introductory guides to C++ all day long.

I am writing a program in Visual C++ 6 SP5.
My code has a lot of "if" statements - 19 to be exact.
If the if statements are true (technically, there should only be one that is
ever true), the program needs to continue on after the if statements.
Each if statement assigns a particular string value to a character/string
array if the if statement is true.

Unfortunately, I have the problem whereby only my 19th if statement is ever
true. The rest never become true no matter what changes I make to my
variables.

I know using one big "switch" would be appropriate here, but is it possible
to use switch with a float as its condition? My Visual C++ compiler didn't
like it when I used that.

Apologies for so many questions... any help would be appreciated.

Thanks!


 
Reply With Quote
 
 
 
 
Clark Cox
Guest
Posts: n/a
 
      01-04-2004
In article <bt7mk1$42ibn$(E-Mail Removed)-berlin.de>,
"root" <(E-Mail Removed)> wrote:

> Hi group,
>
> Apologies in advance if this has been asked somewhere before, but I haven't
> managed to get anything from the Google archives - I've been getting
> introductory guides to C++ all day long.
>
> I am writing a program in Visual C++ 6 SP5.
> My code has a lot of "if" statements - 19 to be exact.
> If the if statements are true (technically, there should only be one that is
> ever true), the program needs to continue on after the if statements.
> Each if statement assigns a particular string value to a character/string
> array if the if statement is true.
>
> Unfortunately, I have the problem whereby only my 19th if statement is ever
> true. The rest never become true no matter what changes I make to my
> variables.
>
> I know using one big "switch" would be appropriate here, but is it possible
> to use switch with a float as its condition? My Visual C++ compiler didn't
> like it when I used that.
>
> Apologies for so many questions... any help would be appreciated.
>
> Thanks!
>
>


Although I can't be sure, as your were rather vague about what the
conditions these if statements are checking for are, it sounds to me
like you're trying to test floating point values for equality, which is
a big no-no. Check out:

http://www.parashift.com/c++-faq-lit...html#faq-29.17

 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      01-04-2004
"root" <(E-Mail Removed)> wrote in message
news:bt7mk1$42ibn$(E-Mail Removed)-berlin.de
> Hi group,
>
> Apologies in advance if this has been asked somewhere before, but I
> haven't managed to get anything from the Google archives - I've been
> getting introductory guides to C++ all day long.
>
> I am writing a program in Visual C++ 6 SP5.
> My code has a lot of "if" statements - 19 to be exact.
> If the if statements are true (technically, there should only be one
> that is ever true), the program needs to continue on after the if
> statements.
> Each if statement assigns a particular string value to a
> character/string array if the if statement is true.
>
> Unfortunately, I have the problem whereby only my 19th if statement
> is ever true. The rest never become true no matter what changes I
> make to my variables.
>


You obviously have a bug in your code.

> I know using one big "switch" would be appropriate here, but is it
> possible to use switch with a float as its condition?


No it isn't.

Post your code (in compileable form) and people here will

1. Find the bug.
2. Possibly suggest a more elegant solution.


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)
 
Reply With Quote
 
Christof
Guest
Posts: n/a
 
      01-04-2004
root wrote:

> Hi group,
>
> Apologies in advance if this has been asked somewhere before, but I haven't
> managed to get anything from the Google archives - I've been getting
> introductory guides to C++ all day long.
>
> I am writing a program in Visual C++ 6 SP5.
> My code has a lot of "if" statements - 19 to be exact.
> If the if statements are true (technically, there should only be one that is
> ever true), the program needs to continue on after the if statements.
> Each if statement assigns a particular string value to a character/string
> array if the if statement is true.
>
> Unfortunately, I have the problem whereby only my 19th if statement is ever
> true. The rest never become true no matter what changes I make to my
> variables.
>
> I know using one big "switch" would be appropriate here, but is it possible
> to use switch with a float as its condition? My Visual C++ compiler didn't
> like it when I used that.
>
> Apologies for so many questions... any help would be appreciated.
>
> Thanks!
>
>

Hello nameless poster,

we can't help you without seeing your code, so please try to minimize
the problem and post your code here.

--
Regards,
Christof Krueger

 
Reply With Quote
 
root
Guest
Posts: n/a
 
      01-04-2004
"root" <(E-Mail Removed)> wrote in message
news:bt7mk1$42ibn$(E-Mail Removed)-berlin.de...

Thanks for all your replies so far.

I can't post the entire block of code here for commercial reasons, but a
selection of the if statements are:

if(difference == 0.000) {
strcpy(PFM_Date,"A5"); }

if(difference == 0.052 || 0.053) {
strcpy(PFM_Date,"M25"); }

if(difference == 0.894 || 0.895) {
strcpy(PFM_Date,"M29"); }

if(difference == 0.947 || 0.94 {
strcpy(PFM_Date,"A17"); }

where:

difference is of type float
and PFM_Date[4] of type char.

I am trying to set PFM_Date to a three-letter code if the conditions I've
posted have been met. Only one of these many if statements will ever be
true. My source file is of type .cpp in VC++ 6, SP5.

TIA.


 
Reply With Quote
 
Christof Krueger
Guest
Posts: n/a
 
      01-04-2004
root wrote:

> "root" <(E-Mail Removed)> wrote in message
> news:bt7mk1$42ibn$(E-Mail Removed)-berlin.de...
>
> Thanks for all your replies so far.
>
> I can't post the entire block of code here for commercial reasons, but a
> selection of the if statements are:
>
> if(difference == 0.000) {
> strcpy(PFM_Date,"A5"); }
>
> if(difference == 0.052 || 0.053) {
> strcpy(PFM_Date,"M25"); }
>
> if(difference == 0.894 || 0.895) {
> strcpy(PFM_Date,"M29"); }
>
> if(difference == 0.947 || 0.94 {
> strcpy(PFM_Date,"A17"); }

What you are doing here is the following:

if ( (difference == 0.947) || (0.94 ) {
strcpy(PFM_Date,"A17"); }

That means that your if-condition is true when either (difference ==
0.947) is true, or if (0.94 is true. In C++ numeric values that are
not 0 _always_ return true.
In your posted code the last three strcpy-statements are executed, but
you only see the effect of the last one, because it overwrites the
contents of PFM_Date.
What you probably mean is

if ( (difference == 0.947) || (difference == 0.94 ) {
strcpy(PFM_Date,"A17"); }


Another _very_ important point: Never check float values for equality.
There is no exact binary representation for the number 0.01 for example.
So you should rather use if-statements in the following form:

if ( (difference >= 0.947) || (difference < 0.94 ) {
strcpy(PFM_Date,"A17"); }

And even this can lead to wrong results because of rounding.

--
Regards,
Christof Krueger

 
Reply With Quote
 
david m-
Guest
Posts: n/a
 
      01-04-2004
Not convinced that any of the sample given evaluate to true!

However,

if (difference == 0.947 || 0.94

== has a higher precedence than || so the expression is

(difference == 0.947) || 0.948

the equality operator returns true of false
the boolean OR operator || returns true or false.

What you are presumably trying to type is

(difference == 0.947) || (difference == 0.94

but I'd be very careful of testing floating point values in this way.

Would something like

(difference >= 0.947) && (difference <= 0.94

suffice?

davd m.

"root" <(E-Mail Removed)> wrote in message
news:bt9idh$4b0l2$(E-Mail Removed)-berlin.de...
> "root" <(E-Mail Removed)> wrote in message
> news:bt7mk1$42ibn$(E-Mail Removed)-berlin.de...
>
> Thanks for all your replies so far.
>
> I can't post the entire block of code here for commercial reasons, but a
> selection of the if statements are:
>
> if(difference == 0.000) {
> strcpy(PFM_Date,"A5"); }
>
> if(difference == 0.052 || 0.053) {
> strcpy(PFM_Date,"M25"); }
>
> if(difference == 0.894 || 0.895) {
> strcpy(PFM_Date,"M29"); }
>
> if(difference == 0.947 || 0.94 {
> strcpy(PFM_Date,"A17"); }
>
> where:
>
> difference is of type float
> and PFM_Date[4] of type char.
>
> I am trying to set PFM_Date to a three-letter code if the conditions I've
> posted have been met. Only one of these many if statements will ever be
> true. My source file is of type .cpp in VC++ 6, SP5.
>
> TIA.
>
>



 
Reply With Quote
 
root
Guest
Posts: n/a
 
      01-04-2004
"Christof Krueger" <(E-Mail Removed)> wrote in message
news:bt9j4m$dvq$07$(E-Mail Removed)-online.com...
> root wrote:
>
> > "root" <(E-Mail Removed)> wrote in message
> > news:bt7mk1$42ibn$(E-Mail Removed)-berlin.de...

> That means that your if-condition is true when either (difference ==
> 0.947) is true, or if (0.94 is true. In C++ numeric values that are
> not 0 _always_ return true.
> In your posted code the last three strcpy-statements are executed, but
> you only see the effect of the last one, because it overwrites the
> contents of PFM_Date.
> What you probably mean is
>
> if ( (difference == 0.947) || (difference == 0.94 ) {
> strcpy(PFM_Date,"A17"); }


Thanks for that. I've tried it and now PFM_Date is coming out as blank
(it's being printed using a printf).

> Another _very_ important point: Never check float values for equality.
> There is no exact binary representation for the number 0.01 for example.
> So you should rather use if-statements in the following form:
>
> if ( (difference >= 0.947) || (difference < 0.94 ) {
> strcpy(PFM_Date,"A17"); }
>
> And even this can lead to wrong results because of rounding.


I know this is really bad programming, but I must check for those specific
values. Maybe C++ isn't the best language for this.

Thanks again anyway.


 
Reply With Quote
 
Christof Krueger
Guest
Posts: n/a
 
      01-04-2004
root wrote:

> "Christof Krueger" <(E-Mail Removed)> wrote in message
> news:bt9j4m$dvq$07$(E-Mail Removed)-online.com...
>
>>root wrote:
>>
>>
>>>"root" <(E-Mail Removed)> wrote in message
>>>news:bt7mk1$42ibn$(E-Mail Removed)-berlin.de...

>>
>>That means that your if-condition is true when either (difference ==
>>0.947) is true, or if (0.94 is true. In C++ numeric values that are
>>not 0 _always_ return true.
>>In your posted code the last three strcpy-statements are executed, but
>>you only see the effect of the last one, because it overwrites the
>>contents of PFM_Date.
>>What you probably mean is
>>
>> if ( (difference == 0.947) || (difference == 0.94 ) {
>> strcpy(PFM_Date,"A17"); }

>
>
> Thanks for that. I've tried it and now PFM_Date is coming out as blank
> (it's being printed using a printf).

It's blank because none of the if-statements ever are true (I assume
because you test float numbers for equality).
Do you really want to test for the *exact* value 0.947 or the *exact*
value 0.948? Should the test fail for 0.9475? Or for 0.94700001? And
what's with 0.94699999?



--
Regards,
Christof Krueger

 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      01-04-2004

"root" <(E-Mail Removed)> wrote in message
news:bt9k3o$4o97p$(E-Mail Removed)-berlin.de...
> I know this is really bad programming, but I must check for those specific
> values. Maybe C++ isn't the best language for this.
>

Checking for exact floating point values is not only bad programming in C++,
it's bad programming in most other languages. Try the same thing with
Fortran, C, BASIC, Pascal -- you'll get the same problems.

What you need is to either use a library that gives you more precision
(usually these libraries are used for business calculations, where exact
arithmetic is mandatory), or use a language that has exact representation of
such numbers. Maybe COBOL.

Paul




 
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
if statements and case statements questions John Crichton Ruby 6 07-12-2010 06:17 PM
[False,True] and [True,True] --> [True, True]????? bdb112 Python 45 04-29-2009 02:35 AM
Prepare Statements VS Statements Vince Java 12 01-21-2008 01:18 PM
component statements within architecture statements Neil Zanella VHDL 8 10-20-2006 09:05 AM
if statements with or w/o else statements Harry George Python 6 02-23-2004 06:48 PM



Advertisments