473,396 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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!
Jul 22 '05 #1
14 3398
In article <bt************@ID-193745.news.uni-berlin.de>,
"root" <ae***@despammed.com> 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

Jul 22 '05 #2
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

Jul 22 '05 #3
"root" <ae***@despammed.com> wrote in message
news:bt************@ID-193745.news.uni-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)
Jul 22 '05 #4
"root" <ae***@despammed.com> wrote in message
news:bt************@ID-193745.news.uni-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.948) {
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.
Jul 22 '05 #5
root wrote:
"root" <ae***@despammed.com> wrote in message
news:bt************@ID-193745.news.uni-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.948) {
strcpy(PFM_Date,"A17"); }

What you are doing here is the following:

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

That means that your if-condition is true when either (difference ==
0.947) is true, or if (0.948) 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.948) ) {
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.948) ) {
strcpy(PFM_Date,"A17"); }

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

--
Regards,
Christof Krueger

Jul 22 '05 #6
Not convinced that any of the sample given evaluate to true!

However,

if (difference == 0.947 || 0.948)

== 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.948)

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

Would something like

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

suffice?

davd m.

"root" <ae***@despammed.com> wrote in message
news:bt************@ID-193745.news.uni-berlin.de...
"root" <ae***@despammed.com> wrote in message
news:bt************@ID-193745.news.uni-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.948) {
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.

Jul 22 '05 #7
"Christof Krueger" <ne**@pop2wap.net> wrote in message
news:bt*************@news.t-online.com...
root wrote:
"root" <ae***@despammed.com> wrote in message
news:bt************@ID-193745.news.uni-berlin.de... That means that your if-condition is true when either (difference ==
0.947) is true, or if (0.948) 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.948) ) {
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.948) ) {
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.
Jul 22 '05 #8
root wrote:
"Christof Krueger" <ne**@pop2wap.net> wrote in message
news:bt*************@news.t-online.com...
root wrote:

"root" <ae***@despammed.com> wrote in message
news:bt************@ID-193745.news.uni-berlin.de...


That means that your if-condition is true when either (difference ==
0.947) is true, or if (0.948) 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.948) ) {
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

Jul 22 '05 #9

"root" <ae***@despammed.com> wrote in message
news:bt************@ID-193745.news.uni-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


Jul 22 '05 #10
On Sun, 04 Jan 2004 17:27:39 +0000, root wrote:
"root" <ae***@despammed.com> wrote in message
news:bt************@ID-193745.news.uni-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.948) {
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.


Sounds like you need fixed point (exact) arithmetic, not floating point.
The precision of floating point is not exact, you need to compare to some
delta. In your case it seems like a delta of 0.0005 is adequate, but
without the exact requirements, one cannot tell.

Fixed point arithmetic is not directly supported by C, but you my want to
look into counting promilles directly in a long.

HTH,
M4

Jul 22 '05 #11
"Christof Krueger" <ne**@pop2wap.net> wrote in message
news:bt*************@news.t-online.com...
root wrote:
"Christof Krueger" <ne**@pop2wap.net> wrote in message
news:bt*************@news.t-online.com...
"root" <ae***@despammed.com> wrote in message
news:bt************@ID-193745.news.uni-berlin.de...

That means that your if-condition is true when either (difference ==
0.947) is true, or if (0.948) 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.948) ) {
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?


I'm basically comparing the result of a calculation (a division to be exact)
against a table of alphanumeric codes. 0.9475 needs to be taken as 0.947
with regards to what I need, as I only need the first three digits.

Would it be a better idea, if I managed to do the division, get the decimal
values, and convert them to integers, thus losing the remaining decimal
values and not having to worry about if statements and float variables? As
another poster pointed out, the OR part in the if statement is there to
cover me for rounding errors (not really full-proof).

For example, if after the division I end up with 158.947123456780.......,
would it be a better idea (perhaps more robust?) to get 0.947 by taking the
difference of the above number and the division using integer division (as I
am currently doing), then multiplying 0.947 by, say 100, to get 947 and
putting it in another, integer type variable?

Thanks again to you all for your replies. Apologies for the slightly
complicated post!
Jul 22 '05 #12
"Paul" <pa**@paul.com> wrote in message
news:36******************************@news.1usenet .com...

"root" <ae***@despammed.com> wrote in message
news:bt************@ID-193745.news.uni-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.
Perl perhaps?
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.


What, you mean the Complete and Obsolete Business Orientated Business
Language? ;-)
Jul 22 '05 #13
root wrote:
"Christof Krueger" <ne**@pop2wap.net> wrote in message
news:bt*************@news.t-online.com...
root wrote:

"Christof Krueger" <ne**@pop2wap.net> wrote in message
news:bt*************@news.t-online.com...

>"root" <ae***@despammed.com> wrote in message
>news:bt************@ID-193745.news.uni-berlin.de...

That means that your if-condition is true when either (difference ==
0.947) is true, or if (0.948) 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.948) ) {
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?

I'm basically comparing the result of a calculation (a division to be exact)
against a table of alphanumeric codes. 0.9475 needs to be taken as 0.947
with regards to what I need, as I only need the first three digits.

Would it be a better idea, if I managed to do the division, get the decimal
values, and convert them to integers, thus losing the remaining decimal
values and not having to worry about if statements and float variables? As
another poster pointed out, the OR part in the if statement is there to
cover me for rounding errors (not really full-proof).

For example, if after the division I end up with 158.947123456780.......,
would it be a better idea (perhaps more robust?) to get 0.947 by taking the
difference of the above number and the division using integer division (as I
am currently doing), then multiplying 0.947 by, say 100, to get 947 and
putting it in another, integer type variable?

I'm sure 100 was a typo and you mean 1000. But that is what I would have
suggested. You could cast it to int (I think that always truncates
towards zero, but correct me if I am wrong) and then check against your
hard-coded values to do whatever you want to do.
Because rounding errors can occur with FP arithmetics very easily, you
probably still should check for ranges rather than for fixed numbers.

What you would do at the moment is

float a = <whatever>;
float b = <whatever>;
float c = a / b; // rounding errors!
c -= (int)c // works well assuming you don't have negative numbers
int d = (int)(c * 1000)
with d containing numbers from 0 to 999

If a and b are integral values, you could even do the following which is
equivalent:

int a = <whatever>;
int b = <whatever>;
int c = a % b; // modulo
int d = (c * 1000) / b; // integer division rounds down (truncates)

This code should be faster on common architectures (no, i can't prove
it), and should be more predictible concerning rounding errors because
no architecture dependent FP errors can occur. The only assumption made
here is that integer division rounds towards zero.

If you can rewrite your code like this depends on the nature of your
calculation. With integer arithmetic you do not have rounding errors,
but you should be very careful not to let your variables overflow. (e.g.
the operation 70000*65000 already overflows an unsigned 32bit integer!)
Thanks again to you all for your replies. Apologies for the slightly
complicated post!

--
Regards,
Christof Krueger

Jul 22 '05 #14
"Christof Krueger" <ne**@pop2wap.net> wrote in message
news:bt*************@news.t-online.com...
"Christof Krueger" <ne**@pop2wap.net> wrote in message
news:bt*************@news.t-online.com...

"Christof Krueger" <ne**@pop2wap.net> wrote in message
news:bt*************@news.t-online.com...
>>"root" <ae***@despammed.com> wrote in message
>>news:bt************@ID-193745.news.uni-berlin.de... For example, if after the division I end up with 158.947123456780......., would it be a better idea (perhaps more robust?) to get 0.947 by taking the difference of the above number and the division using integer division (as I am currently doing), then multiplying 0.947 by, say 100, to get 947 and
putting it in another, integer type variable?

I'm sure 100 was a typo and you mean 1000. But that is what I would have
suggested. You could cast it to int (I think that always truncates
towards zero, but correct me if I am wrong) and then check against your
hard-coded values to do whatever you want to do.
Because rounding errors can occur with FP arithmetics very easily, you
probably still should check for ranges rather than for fixed numbers.
Hi again.
Yes, it was a typo! tried it with 1,000 yesterday and it worked a treat. I
did something similar to what you recommended below, though not in as few
lines as you did.
All's well that ends well.
My thanks to the group, especially to you, Christof. You've been a great
help.

Hapy 2004 to you all!

What you would do at the moment is

float a = <whatever>;
float b = <whatever>;
float c = a / b; // rounding errors!
c -= (int)c // works well assuming you don't have negative numbers
int d = (int)(c * 1000)
with d containing numbers from 0 to 999

If a and b are integral values, you could even do the following which is
equivalent:

int a = <whatever>;
int b = <whatever>;
int c = a % b; // modulo
int d = (c * 1000) / b; // integer division rounds down (truncates)

This code should be faster on common architectures (no, i can't prove
it), and should be more predictible concerning rounding errors because
no architecture dependent FP errors can occur. The only assumption made
here is that integer division rounds towards zero.

If you can rewrite your code like this depends on the nature of your
calculation. With integer arithmetic you do not have rounding errors,
but you should be very careful not to let your variables overflow. (e.g.
the operation 70000*65000 already overflows an unsigned 32bit integer!)

Jul 22 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I...
3
by: Brian L. | last post by:
After following the discussion on Generator expressions on python-dev for the last few weeks, and finally getting used to list comprehensions as opposed to map/filter, it occurred to me that the...
0
by: David.Tymon | last post by:
>Description: MySQL v4.1.0-alpha only allows a client to prepare a maximum of 254 statements. On the 255th mysql_prepare() call, a failure is returned with no information returned by...
35
by: David Cleaver | last post by:
Hello all, I was wondering if there were some sort of limitations on the "if" statement? I'm writing a program which needs to check a bunch of conditions all at the same time (basically). And...
39
by: slogging_away | last post by:
Hi - I'm running Python 2.4.2 (#67, Sep 28 2005, 12:41:11) on win32, and have a script that makes numerous checks on text files, (configuration files), so discrepancies can be reported. The script...
28
by: entfred | last post by:
I have the following line of html: &nbsp;&nbsp1234&nbsp;&nbsp;&nbsp;&nbsp;&nbspabc&nbsp;&nbsp;&nbspyow In Internet Explorer 6.0, the columns look ok using the above html: 1234 abcd ...
5
by: sam_cit | last post by:
Hi Everyone, I read somewhere that there are some compile time operations behind switch-case, which is why it can work for cases which evaluates to an integer or character and not strings and...
21
by: K Viltersten | last post by:
In the code at our company i see the following. if (someStuff != null) { if (someStuff != "") { doThatThing (); } } While it's fully correct and valid, i'd like to rewrite it as follows.
11
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Can switch statements be nested? I've got a large routine that runs off of a switch statement. If one of the switches in switch #1 is true, that enters switch statement #2. Some of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.