Function call not working correctly

I am working on a project with a function.
There is an 'If else' that uses a calculation from my function to determine which substance it is according to boiling point of the substance(within 5%)...but for some reason I only get the first If as an answer each time(water).
Does anyone know why this is happening?



#include <stdio.h>
#include "stdafx.h"


int within_x_percent(double ref,double data,double x) ;
double ref;
double data;
double x = .05;

int _tmain(int argc, _TCHAR* argv[])
{


printf("Enter the boiling point of the substance: ");

scanf("%1f", &data);


if (data = within_x_percent(100,data,x))
printf("Your substance is Water.\n");

else if (data = within_x_percent(357,data,x))
printf("Your substance is Mercury. \n");

else if (data = within_x_percent(1187,data,x))
printf("Your substance is Copper. \n");

else if (data = within_x_percent(2193,data,x))
printf("Your substance is Silver. \n");

else if (data = within_x_percent(2660,data,x))
printf("Your substance is Gold. \n");

else
printf("Substance Unknown\n");


return 0;

}


int within_x_percent(double ref,double data,double x)

{


if (( ref - x * ref) <= data <= (ref + x * ref))
return 1;
else
return 0;

}

= is the assignment operator.
== is the equality (check) operator.
I don't see where the equality operator is required...?
In the conditions
if (data == within_x_percent(100,data,x))

1
2
x = n; //set n to be the new value for x
x == n; //returns whether x is equal to n 




http://www.cplusplus.com/doc/tutorial/operators.html
Last edited on
Ah...I see. Thank you.
I fixed that but now I get "substance unknown" for anything I input.
if (( ref - x * ref) <= data <= (ref + x * ref)) is wrong too, it should be
if (( ref - x * ref) <= data || data <= (ref + x * ref))
Last edited on
ok, I changed it to:
if (data >=( ref - x * ref) && data <= (ref + x * ref))

Because it has to be within +/- 5%

but I still get the same error.
Oh, I didn't noticed that you are comparing 'data' to a boolean, the condition should only be like this:
if ( within_x_percent(100,data,x) )
Last edited on
within_x_percent() returns an int specifying whether a condition is true or false. Why are you comparing this to data? Remove the comparisons and just leave every if like this: if (within_x_percent(100,data,x))
lol whoops.
I am still getting "Substance unknown" for anything I input though.

Here is the updated code:


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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include "stdafx.h"

int within_x_percent(double ref,double data,double x) ;
double ref;
double data;
double x = .05;

int _tmain(int argc, _TCHAR* argv[])
{


printf("Enter the boiling point of the substance: "); 

scanf("%1f", &data);	
 
 
       if (within_x_percent(100,data,x))
          printf("Your substance is Water.\n");

       else if (within_x_percent(357,data,x))
         printf("Your substance is Mercury. \n");
	
       else if (within_x_percent(1187,data,x))
		   printf("Your substance is Copper. \n");

       else if (within_x_percent(2193,data,x))
          printf("Your substance is Silver. \n");

       else if (within_x_percent(2660,data,x))
          printf("Your substance is Gold. \n");

       else 
          printf("Substance Unknown\n"); 


 return 0;
 
  }


int within_x_percent(double ref,double data,double x)

      {
       

        if (data >=( ref - x * ref) && data <= (ref + x * ref))
			return 1;
		else
			return 0;

      }
%1f reads a float. data is a double.

do you mean %lf instead?
LOL! wow.
...thank you all.
It works fine now. :)
Topic archived. No new replies allowed.