How to used a integer in a loop if

Hi all,

i need to add another possibility In the code ( cf below) . There is 3 cases here :
- good beta = -4/3
- not good beta = -5/3
- fairly good beta = -2

In this case i can not use a boolean. So maybe an integer it is a good idea, but i do not know how.

I hope I am enough clear.

Thanks for your answer



I have this code.

1
2
3
4
5
6
7
8
9
10
11
12
13
    
  if(aFeat.compare("good")){
    good=true;
    beta=-4.0/3.0;
   
  }

  
    else{
    good=false;
    beta=-5.0/3.0;
    
  }



Hello Efalir,

Making a guess here, but I think what you want is:

1
2
3
4
5
6
7
8
9
10
11
12
if(aFeat.compare == "good")
{
    // Code
}
else if (aFeat.compare == "not good")
{
    // Code
}
else   //  for fair  Or continue with the "else if" for more.
{
    //Code
}


Hope that helps,

Andy
Not enough information. What is aFeat? Is it a std::string or your own class? If your own class, what does compare return?

std::string.compare returns 0 if the string is equal to the argument. If aFeat is a std::string, you're testing not equal "good".

You don't make clear what condition(s) must be true for "fairly good beta".

You can use an enum to represent three possible values.
 
enum good_enm { GOOD, NOT_GOOD, FAIRLY_GOOD };
Last edited on
not sure if new c++ has a tri-state or not but that aside, you can do it generally with 2 approaches.

first let me comment that you want to be very careful here with equality against floating point values.

approach 1
enum quality
{
good, fairly_good, not_good, max_quality
};
quality q;
if(condition)
q = good;
else
if(other condition)
q = 'not_good'
etc...
you can use a switch off q, or comparisons.

approach 2:
unsigned char superbool;

if(condition)
superbool = 1;
else if(condition)
superbool = 2;
else
superbool = 4; //important

now you can use bitwise operators against the 000111 bits
which is sometimes useful. You do this if you can have 2 of the conditions at once, for example superbool & 3 checks both 1 and 2, or superbool & 5 checks 4 & 1, etc.
This is less intuitive than the enum but it allows you to simplify combined conditions to a single condition.


you can *combine* these techniques by assigning powers of 2 in the enum (default enums start at 0 and increment 1,2,3,..)

{good = 1, fairly_good = 2, not_good = 4, ...}




Last edited on
Hi Handy Andy


i tried your method, but it do not seem to work. Because aFeat is an std::string.

[code]

// featuring good
if(aFeat.compare("good")){
good=1;
beta=-4.0/3.0;

} else if(aFeat.compare("not good")) {
good=0;
beta=-5.0/3.0;

} else {
=-1;
beta=-2.0;
}


Hi AbstractioAnon, aFeat is indeed an std::string. I m goind to try your method. But i never used enum..

Hi Jonnin, thank for your answer. I need more time to understand. :)
As I noted before, std::string.compare returns 0 if the strings are equal. Your comparisons are testing not equal to zero, therefore your comparisons fail.

There is a more intuitive way to compare strings using the == operator.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
enum good_enm { GOOD, NOT_GOOD, FAIRLY_GOOD };

good_enm   result;

  if (aFeat == "good")
  {   result = GOOD;
      beta = -4.0/3.0;
  } 
  else 
  if (aFeat == "not good") 
  {   result = NOT_GOOD;
      beta = -5.0/3.0;
  } 
  else 
  {  result = FAIRLY_GOOD;
      beta=-2.0;
  }



Last edited on
I tried. But i have a message :

../src/cstrings.cc:45:1: error: ‘good_enum’ was not declared in this scope

../src/cstrings.cc:48:7: error: ‘result’ was not declared in this scope

I m new ic c++. I try to modify a code in c++ for the work. I m student in physics ..


Hello Efalir,

This:
1
2
3
4
5
6
7
8
9
10
11
12
13
// featuring good
if(aFeat.compare("good")){
good=1;
beta=-4.0/3.0;

} else if(aFeat.compare("not good")) {
good=0;
beta=-5.0/3.0;

} else {
=-1;
beta=-2.0;
}

is not what I showed you.

I accepted that "aFeat.compare" is something that points to a std::string. Unless I have missed something with the standards past C++11 aFeat.compare("not good") is not the way to compare two strings.

I gave you a start that you could finish.

Look back at what I showed you and see what the difference is between the two.

Hope that helps,
chained if statements is great for 3. The methods I was showing are general purpose for places where you might have 5 or 8 or whatever numbers of categories to handle.

If you want to chain if statements you only need 2 to cover 3:

q = not_good;
if(condition)
q = good;
else if (othercondition)
q = fairly_good;

if neither if statement happens, it was initialized to not_good and retains that value as a default.

enums just name the conditions so they are easy to read and use in code. It is identical to use of an integer. Using the binary / bitwise logic is identical to using an integer or an enum, with the difference being that you only use powers of 2 which turns each bit of the state into a boolean, effectively, and again this allows you to do simplify some statements if you have multi-condition statements (this is not normally worth doing until the # of states reaches 4 or more, otherwise negative logic is sufficient, that is if you want 1 and 2, then you can pick not 3 instead).




Hello Efalir,

I see what you are doing now, but you are still using the "str.compare()" wrong. you code should, if I understand it correctly, should be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// featuring good
if(aFeat.compare("good") == 0)
{
good = 1;
beta = -4.0/3.0;
}
else if(aFeat.compare("not good") == 0)
{
good = 0;
beta = -5.0/3.0;
}
 else
{
 = -1;  // something is need for lhs of =.
beta = -2.0;
}


I believe this should work, but I need to test it.

Hope that helps,

Andy
I tried. But i have a message :

../src/cstrings.cc:45:1: error: ‘good_enum’ was not declared in this scope

Check your spelling. I named the enum good_enm (no u). Your error message indicates you included a u.
Hi Handy Andy, Thanks for your answer! It works well ;)

HI Jonnin, Thank you for the information, it was really helpful to understand.

Hi AbstractionAnon, ok i see, i m stupid :), i ll try one again.

Topic archived. No new replies allowed.