booleans

I have a problem about booleans. if 'begin' and 'eind' are true, then checker must be true too. But it isn't. what is the problem?

'aantalp' is a integer variable declared in the private section of the class
'maten' is a two-dimensional array, containing char arrays
'eenheden' is a one-dimensional array, containing floats

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
bool berekening::checken(char *cbegin, char *ceind)
{
     int count=0;
     bool begin = false;
     bool eind = false;
     bool checker = false;
     for(count=0; count<aantalp; count++)
     {
          if(strcmp(cbegin, maten[count])==0)
          {
               begin=true;
               beginmaat=eenheden[count];
          }//end if
          else{begin=false;}
          if(strcmp(ceind, maten[count])==0)
          {
               eind=true;
               eindmaat=eenheden[count];
          }
          else{eind=false;}//end if
     }//next count
     
     if(begin==true && eind==true)
     {
          checker=true;              
     }
     
     return checker;



Last edited on
The problem must be elsewhere.

Why don't you just

 
return begin && eind;


instead?
it doesn't works
The output of this function will depend on the result of the last comparison of the for loop.
By that I mean, if count goes from 0 to 16 - then the setting of eind and begin
for count of 0 - 15 will have no effect on the result.

It is only the setting of eind and begin in the last iteration of the loop that will be used
to determine the value of checker .

This is because all the iterations of the loop are run.

I'm guessing that maybe you need to exit the loop and return true the first time both eind and begin becomes true and if you go all the way through the iterations of the loop, then return false.

Maybe this is what your loop should look like (it is just a guess):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
bool berekening::checken(char *cbegin, char *ceind)
{
     int count=0;
     bool begin = false;
     bool eind = false;
     bool checker = false;
     for(count=0; count<aantalp; count++)
     {
          if(strcmp(cbegin, maten[count])==0)
          {
               begin=true;
               beginmaat=eenheden[count];
          }//end if
          else{begin=false;}
          if(strcmp(ceind, maten[count])==0)
          {
               eind=true;
               eindmaat=eenheden[count];
          }
          else{eind=false;}//end if
          
         if(begin && eind)
        {
           checker = true;
           break;
         }

     }//next count
     
     
     return checker;
}
i got it, thanks! I think i'm going to remove the else-statement, because it is unuseful.
thanks anyway!
Last edited on
Topic archived. No new replies allowed.