invailid operands to binary expression ('float' and 'float)

In the line indicated below I get the error, "invailid operands to binary expression ('float' and 'float). I'm trying to display the radians as "pi radians" for example when the input sides is 4. The output sum of interior angles is 2 pi radians. How can I get rid of this error and accomplish my goal? Thanks for reading.

#include <iostream>
#include <string>
using namespace std;

int main()
{
int sum_angle;
int num_sides;
int r_d; // radians/degrees
char R; //input radians
float radian;
int radian_pi;
float pi=3.14;

cout << "Basic Geometry with c++";
cout << "\n-----------------------\n";
cout << "Enter the number of sides of your polygon (3-10) :\n";
cin >> num_sides;
cout << "display options:\n";
cout << " - Type R for radians\n";
cout << " - Type anything else for degrees\n";
cin >> R;

sum_angle = (((num_sides)-2)*180);
radian = (sum_angle*pi/180);
radian_pi = (radian%pi); // <-- this line is the problem


if (R=='R')
{

cout << "The sum of interior angles in your polygon is:" << radian_pi << "radians";
cout << "\nA polygon with " << num_sides << " sides is called a ";
}

else
{
sum_angle=(((num_sides)-2)*180);

cout << "The sum of interior angles is:"<< sum_angle;
cout << "\nA polygon with " << num_sides << " sides is called a ";

}




if (num_sides==3)
cout << "triangle";

if (num_sides==4)
cout << "quadrilateral or tetragon";

if (num_sides==5)
cout << "pentagon";


return 0;
}
closed account (o3hC5Di1)
Hi there,

The modulus operator %() only works with integers:
http://stackoverflow.com/questions/6102948/why-does-modulus-division-only-work-with-integers

Your radian and pi variables are floats, which will not work with this operator unless you overload it.

On a sidenote, please wrap your code in [code][/code]-tags, it makes it more readable.

Hope that helps.

All the best,
NwN
Thanks I got it all to work out. Although I'm curious what do you mean by tags?
closed account (o3hC5Di1)
If you put your code within the tags:

[code] // code here [/code] it will look like this:

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
53
54
55
56
57
58
59
#include <iostream>
#include <string>
using namespace std;

int main()
{
int sum_angle;
int num_sides;
int r_d; // radians/degrees
char R; //input radians
float radian;
int radian_pi;
float pi=3.14;

cout << "Basic Geometry with c++";
cout << "\n-----------------------\n";
cout << "Enter the number of sides of your polygon (3-10) :\n";
cin >> num_sides;
cout << "display options:\n";
cout << " - Type R for radians\n";
cout << " - Type anything else for degrees\n";
cin >> R;

sum_angle = (((num_sides)-2)*180);
radian = (sum_angle*pi/180);
radian_pi = (radian%pi); // <-- this line is the problem


if (R=='R')
{

cout << "The sum of interior angles in your polygon is:" << radian_pi << "radians";
cout << "\nA polygon with " << num_sides << " sides is called a ";
}

else
{
sum_angle=(((num_sides)-2)*180);

cout << "The sum of interior angles is:"<< sum_angle;
cout << "\nA polygon with " << num_sides << " sides is called a ";

}




if (num_sides==3)
cout << "triangle";

if (num_sides==4)
cout << "quadrilateral or tetragon";

if (num_sides==5)
cout << "pentagon";


return 0;
}


All the best,
NwN
Thanks, I will take advantage of that!
Topic archived. No new replies allowed.