# Thread: Help w/ complicated variable test

1. ## Help w/ complicated variable test

ok i am pretty new to C here, and have limited programming experience. i know you are thinking "oh god here we go," but don't worry. i don't think it will be that bad.

i want to assign a value to the variable "type" based on tests of variables "build" and "feet". "type" should be either 1,2,3, or 0. I set it to 0 to start and my logic is that if none of the tests are true (i.e. build != b, B, h, or H) it will remain 0. the problem is that i can't get it to work out correctly.

Code:
```
int type_calc(build, feet){

int type, type1, type2, type3;
type = 0;

/*if any test evaluates to true, the value
returned for that test is 1.  then it is just a matter of multiplying each different test by a different number.  the tests are mutually exclusive, so you can safely say that each number (1,2,3) corresponds to a different type (copper, coax, twisted
respectively) and 0 is an invalid entry.*/

type = (((build == 'h') || (build == 'H')) && (feet < 20)) +

2*((((build == 'h') || (build == 'H')) && (feet >= 20.0)) ||
(((build == 'b') || (build == 'B')) && (feet < 50.0))) +

3*(((build == 'b') || (build == 'B')) && (feet >= 50.0));

return (type);

}```
what am i doing wrong. i have put a million parantheses to attempt to make it work, but to no avail. i think this is true:

1. test 1 never evaluates to true
2. test 2 never evaluates to true for build = 'b' || 'B'

that's what i've seen. i guess there's something wrong with this code, but i can't figure it out for the life of me...

jungle

2. hm... actually this code looks okay - are you sure you pass the right values to your function?

btw: the types are missing here:
int type_calc(build, feet)
should be e.g.
int type_calc(int build, float feet)

try:
int build = 'h';
float feet = 10.0;
(((build == 'h') || (build == 'H')) && (feet < 20.0)) //oops: .0 was missing.
that should evaluate to true
[/edit]

3. Is this the input/output you would expect?
type_calc('H',10) = 1
type_calc('H',35) = 2
type_calc('H',50) = 2
type_calc('B',10) = 2
type_calc('B',35) = 2
type_calc('B',50) = 3
type_calc('c',10) = 0
Wouldn't it be easier to use a couple of temporaries for 'h' or 'b'? Is there some reason you are not testing feet in an if-tree? And is this the output you want?
Code:
```#include <stdio.h>

int type_calc(int build, double feet)
{
int h = build == 'h' || build == 'H';
int b = build == 'b' || build == 'B';
if ( feet < 20.0 )
{
return h;
}
if ( feet < 50.0 )
{
return 2 * (h || b);
}
return 3 * b;
}

int main(void)
{
int b[] = {'H','B','c'};
double f[] = {10.0, 35.0, 50.0};
size_t i, j;
for ( i = 0; i < sizeof b / sizeof *b; ++i )
{
for ( j = 0; j < sizeof f / sizeof *f; ++j )
{
printf("type_calc('%c',%g) = %d\n", b[i], f[j], type_calc(b[i], f[j]));
}
}

return 0;
}

/* my output
type_calc('H',10) = 1
type_calc('H',35) = 2
type_calc('H',50) = 0
type_calc('B',10) = 0
type_calc('B',35) = 2
type_calc('B',50) = 3
type_calc('c',10) = 0
type_calc('c',35) = 0
type_calc('c',50) = 0
*/```

4. as far as temporaries go, this is for a c class (i hope that doesn't violate any rules here) and we can't use anything we haven't learned yet. basically, it is either this or just a whole lot of if's. the if's would be far simpler, but once i thought of this method, it became a personal vendetta. you know how it goes. it MUST WORK!!!. i'm glad that the code appears to be ok.

the reason i have no variable types is that it messed up the float variable. this is very very odd. i went through and had the program print 'feet' right before it sent it to the function type_calc (no changes afterward) and then inside type_calc. 20.0 became 2.83 something. i dont know what posessed me to remove the float and char, but that made it work to an extent. this is so odd. i am really considering just trying to cut and paste this tiny bit of code into a new file, since i guess there is some weird character throwing it off somewhere that i can't find. i think it's elsewhere in the code.

thanks for the help by the way. any fresh insight is appreciated.

jungle

5. ok i have some input output for you... i just don't understand this. the calling code and beginning of the function follows the in/out. at least the test appears to work.

Scanned... ID:123 Build:b Feet:50.000000
First command of type_calc....
Build: b Feet:3.140625 Type:2

Scanned... ID:124 Build:B Feet:10.000000
First command of type_calc....
Build: B Feet:2.562500 Type:2

Scanned... ID:125 Build:h Feet:20.000000
First command of type_calc....
Build: h Feet:2.812500 Type:1

Scanned... ID:126 Build:h Feet:15.000000
First command of type_calc....
Build: h Feet:2.718750 Type:1

Scanned... ID:127 Build:H Feet:100.000000
First command of type_calc....
Build: H Feet:3.390625 Type:1

Scanned... ID:128 Build:B Feet:50.000000
First command of type_calc....
Build: B Feet:3.140625 Type:2

Scanned... ID:129 Build:B Feet:100.000000
First command of type_calc....
Build: B Feet:3.390625 Type:2

Scanned... ID:130 Build:c Feet:50.000000
First command of type_calc....
Build: c Feet:3.140625 Type:0

Scanned... ID:131 Build:B Feet:100.000000
First command of type_calc....
Build: B Feet:3.390625 Type:2

Scanned... ID:132 Build:h Feet:200.000000
First command of type_calc....
Build: h Feet:3.640625 Type:1

Scanned... ID:133 Build:c Feet:50.000000
First command of type_calc....
Build: c Feet:3.140625 Type:0
Code:
```scanf("%d %c %f", &id, &build, &feet);
printf("Scanned... ID:%d Build:%c Feet:%f\n", id, build, feet);

while(flag != EOF){

type = type_calc(build, feet);```
Code:
```int type_calc(char build, float feet){

printf("\nFirst command of type_calc....\nBuild: %c   Feet:%f", build, feet);```
it would be so great if somebody could explain what the !@#\$% this thing is doing.

jungle