Help aligning

Values seem to get messy after -2, I don't understand why.




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
// computer y in terms of x
// from -4 until 3
#include <iostream>
# include <cmath>
using namespace std;
int main()
{
	double x, result;
	
	cout << "\tX VALUES\tY VALUES\t\tSTATUS" 
	<< endl << endl;
	for (x = -4; x <=3; x= x + 0.5 ) {
	    
		result = (x*x*x*x - x*x*x - (7*(x*x)) + x + 6)/
		              (fabs(x - 3) + sqrt(5 - x));
		
	
	    
		
        cout << '\t' << x << "\t\t" << result; 
			
	    if (result > 0)
	    
	    {cout << "\t\t\tY IS POSITIVE"; 
		cout << endl;
	    }
	    
	    if (result=0)
	    
	    {cout << "\t\t\tY IS ZERO"; 
		cout << endl;
	    }
		
		if (result < 0)
	   
	    {cout << "\t\t\tY IS NEGATIVE"; 
     	cout << endl;
        }


	}

	return 0;
	
}








       X VALUES        Y VALUES                STATUS

        -4              21                      Y IS POSITIVE
        -3.5            11.6497                 Y IS POSITIVE
        -3              5.43698                 Y IS POSITIVE
        -2.5            1.75242                 Y IS POSITIVE
        -2              0       -1.5            -0.398964       -1
0       -0.5            0.673629                        Y IS POSITIVE
        0               1.1459                  Y IS POSITIVE
        0.5             1.01432                 Y IS POSITIVE
        1               0       1.5             -1.94685        2
-4.3923 2.5             -5.67598        3               0
--------------------------------
Process exited with return value 0
Press any key to continue . . .





In function ‘int main()’:
28: warning: suggest parentheses around assignment used as truth value

As well as the error at line 28 pointed out above, it would be better to use setw() to control the width of each column, instead of the tab character.

http://www.cplusplus.com/reference/iomanip/setw/
which compiler are you using that shows an error at line 28? I didnt get any.
The compiler shows a warning, not an error. It takes a human to decide whether the code does what the programmer intended or not.

Which messages you get will depend upon both the compiler, and which options are used when invoking it.

I use these compiler options with gcc (mingw)
-std=c++11 -Wall -Wextra -pedantic
I apologize in advance for these questions but what does this warning mean?
28: warning: suggest parentheses around assignment used as truth value
Last edited on
Well, there are two ways of answering, the first is what is the compiler trying to say, and the second is what should you do about it.

A "truth value" is a value which can be either true or false, in this case it is the value being tested in the if statement. An "assignment" is when a value is assigned to a variable, such as x = -4 or result = 0, in each case the variable on the left of the '=' sign is given the value of whatever expression is to the right of the '='.

The compiler is suggesting that you can fix the problem by adding parentheses. In this case it would mean the code was changed from if (result=0) to if ((result=0)). What does it mean? The compiler is trying to say that the code looks suspicious and is quite probably an error. If you really do want an assignment here, then the extra parentheses will confirm that you meant it and the message will go away. But - that is the wrong action to take - in this case the correct action is to replace the assignment operator '=' with the test for "is equal to", '=='.
Hope that helps.
Last edited on
Great! Thank you very much!
Topic archived. No new replies allowed.