Problem With Math and the Number 181

I'm extremely new to any programming in general. After reading a little bit of the c++ tutorial I decided to make a program to try to test my understanding. I decided to have to program give the area, apothem, radius, side length, and such, given that you know the number of sides and one of those values. There's a lot that it doesn't do that I'll probably try to add later. Upon testing, I found that it worked fine until I set the number of sides to s value over 180. It gives strange results. I also added the interior angle measure and the central angle measure to the results, but they seem always to round to an integer, instead of giving the decimal number.

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <math.h>
#define PI 3.141592653589793238463

using namespace std;
double a, B, A, s, P, r, cena, ina;

void report(int n, double B)
{
	cout << "\n\nCalculating...\n\n\n";
	cout << "Number of Sides: " << n <<"\nApothem: " << a << "\nSide Length: " << s 
                << "\nPerimeter: " << P << "\nRadius: " << r << "\nArea: " << A << "\n\nInterior Angle Measure: " << ina 
                << "\nCentral Angle Measure: " << cena << "\n\n\n";
}

void apothem(int n, double B)
{
	cout << "\n\nWhat is the length of the apothem?\n\n";
	cin >> a;
	s = (tan(B*PI/180)*a)*2;
	P = s*n;
	A = (P*a)/2;
	r = a/cos(B*PI/180);

	report(n, B);
}

void side(int n, double B)
{
	cout << "\n\nWhat is the length of one side?\n\n";
	cin >> s;
	a = (s/2)/tan(B*PI/180);
	P = s*n;
	A = (P*a)/2;
	r = a/cos(B*PI/180);

	report(n, B);
}

void radius(int n, double B)
{
	cout << "\n\nWhat is the length of the radius?\n\n";
	cin >> r;
	s = (r*sin(B*PI/180))*2;
	P = s*n;
	a = (s/2)/tan(B*PI/180);
	A = (P*a)/2;

	report(n, B);
}

void perimeter(int n, double B)
{
	cout << "\n\nWhat is the perimeter?\n\n";
	cin >> P;
	r = ((P/n)/2)/sin(B*PI/180);
	s = (r*sin(B*PI/180))*2;
	a = (s/2)/tan(B*PI/180);
	A = (P*a)/2;

	report(n, B);
}

int main()
{
   int selection, n;
   bool redo;
	cout << "How many sides does the polygon have?\n\n"; //Right now it assumes you know the number of sides. I'll change this later.
	cin >> n;
	B = (360/n)/2;
        cena = 360/n;
        ina = ((n-2)*180)/n;
	cout << "\n\n";
	cout << "What else do you know?\n\n1.Length of the Apothem\n"
                "2.Length of a Side\n3.Length of the Radius\n4.Perimeter of the Polygon\n\nEnter 1-4\n\n";
	cin >> selection;

	if (selection==1)	//sends you to the corresponding function
	apothem(n, B);
	if (selection==2)
	side(n, B);
	if (selection==3)
	radius(n, B);
	if (selection==4)
	perimeter(n, B);
	
	cout << "Would you like to try another?\n\n1.Yes\n2.No\n\n";
	cin >> redo;
	if (redo==true)
	{
	cout << "\n\n\n";
	main();
	}

	return 0;
}


It's terribly sloppy. There's a lot to add and it could definitely use revision, but I just want to know if I'm doing something wrong, whether it's some little detail, or some big stupid mistake.
closed account (o3hC5Di1)
Hi there,

Evilmustard wrote:
I also added the interior angle measure and the central angle measure to the results, but they seem always to round to an integer, instead of giving the decimal number


Try to declare your "n" variable as a double instead and see what that does.
Right now your dividing a double by an integer, which (i think) will cause type conversions.

Sorry I can't be of more help, I'm really poor at Mathematics.

All the best,
NwN
Declaring n as a double worked. Now all the problems seem to be fixed.
Thanks for the help, man.
closed account (o3hC5Di1)
Haha, that was just a guess.

It's kind of strange as I looked op double by integer division and the results showed that a double should be returned, so I'm not sure how this came about.

Perhaps one of the many c++ experts on the forum could tell us what caused the issue here, internally?

Thanks,
NwN
It's because you are calculating them like this:

1
2
cena = 360/n;
ina = ((n-2)*180)/n;


Since that's all ints, you get an integer result.

Also:

 
main();


Don't recursively call main.
Thanks for the explanation and advice.
I'll be doing alot of revision.
Topic archived. No new replies allowed.