multiple if else statement trouble

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
#include <iostream>
#include <conio.h>
#include <cmath>
using namespace std;
int main ()
{
	double x_coordinate, y_coordinate, point;

	cout.setf(ios::fixed);  // fixed decimal position
	cout.setf(ios::showpoint);  // pad with zeroes, if needed
	cout.precision(2);       // round to two decimal places


	cout << "Enter an x coordinate: ";
	cin >> x_coordinate;
	cout << "Enter a y coordinate: ";
	cin >> y_coordinate;

	point = ((x_coordinate * x_coordinate) + (y_coordinate * y_coordinate));

	cout << x_coordinate << " squared plus " << y_coordinate << " squared is " << point << endl;

	
	if (point > 3.0)
		cout << "The point (" << x_coordinate << "," << y_coordinate << ") you are in the white area" << endl;
	else 
	{
		if (point = 3.0)
		cout << "The point (" << x_coordinate << "," << y_coordinate << ") is in second ring" << endl;
		else if (3.0 > point > 2.0)
		cout << "The point (" << x_coordinate << "," << y_coordinate << ") is in second ring" << endl;
		else if (point = 2.0)
		cout << "The point (" << x_coordinate << "," << y_coordinate << ") is in first ring" << endl;
		else if (2.0 > point > 1.0)
		cout << "The point (" << x_coordinate << "," << y_coordinate << ") is in first ring" << endl;
		else if (point = 1.0)
		cout << "The point (" << x_coordinate << "," << y_coordinate << ") is in the middle" << endl;
		else (point < 1.0)
		cout << "The point (" << x_coordinate << "," << y_coordinate << ") is in the middle" << endl;
		
	}
	getch ();
	return 0;
}



Im first getting these errors, but when it does work it either displays all of the statements or none of them.

Also getting these errors

1>Compiling...
1>Dartboard.cpp
1>n:\console application\console applicatio\dartboard.cpp(32) : warning C4804: '>' : unsafe use of type 'bool' in operation
1>n:\console application\console applicatio\dartboard.cpp(36) : warning C4804: '>' : unsafe use of type 'bool' in operation
1>n:\console application\console applicatio\dartboard.cpp(41) : error C2146: syntax error : missing ';' before identifier 'cout'
1>Build log was saved at "file://n:\console application\console applicatio\Debug\BuildLog.htm"
1>console applicatio - 1 error(s), 2 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Any help?
else has no parameter
your error
else (point < 1.0)

use else if
or remove parameter
Ok i fixed that but now its giving me the wrong answer

when i type in 1 for x coodrinate and 1 for y coordinate
point =2

It displays that my point is in the second ring which is false
this format will take care of the warnings:
else if (3.00 > point && point > 2.00)

else if (2.00 > point && point > 1.00)
should remove error.
( point == 3.0 )
and the same to 2.0 and 1.0

make it compare ( == ) instead equal to ( = ).
Last edited on
Probably not a good idea to use == with floating point values.

Floating points are not very likely to have the exact same value as another floating point. Even if the user enters "3.0", that might not equal exactly 3.0 (and thus a == 3.0 check might fail unexpectedly). The reasons for this are because of the way floating points are handled. I don't really care to get into it in detail, but you should know about it.

Anyway... you're generally better off with sticking to range checks. Like >=, <=, >, < when dealing with floating points.

You can do this with a simple else if chain, not unlike your original post:

1
2
3
4
5
6
7
8
if(point >= 3.0)
  cout << "point >= 3.0";
else if(point >= 2.0)
  cout << "3.0 > point >= 2.0";
else if(point >= 1.0)
  cout << "2.0 > point >= 1.0";
else
  cout << "1.0 > point";


BettyBoop's suggestion of doing this:

else if (3.00 > point && point > 2.00)

will also work, but is redundant. The else already ensures that point <= 3.0, so there's no reason to check for it again.
Last edited on
thanks Disch, I didn't know that == was not a good idea with floats :)
Topic archived. No new replies allowed.