Click to See Complete Forum and Search --> : Help, please


CCollin
December 10th, 2009, 10:42 AM
Hi everyone, I'm new to C++, have been using it for just over 2 weeks. I'm working on this program and have gotten several errors. I;ve already fixed the simpler ones i could recognize myself but dont know what to do with the rest, so your help would be greatly appreciated.

#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

void coordinates (float x[50], float y[50], int);
void distance (const float x[50], const float y[50], float space[50], int);
void perimeter (const float space[50], int);
void area (const float x[50], const float y[50],int, float);
void centroidx (const float x[50], const float y[50], int, float &, float);
void centroidy (const float x[50], const float y[50], int, float &, float);

int main()
{
char ABCD;
int state=1;

while (state==1)
{
cout<<"###################################################"<<endl;
cout<<"###################################################"<<endl;
cout<<" Swimming Pool Characteristics "<<endl;
cout<<"___________________________________________________"<<endl;
cout<<endl;
cout<<"Please select one of the options below by selecting"<<endl;
cout<<"its corrseponding number. "<<endl;
cout<<endl;
cout<<"(1) Show the co-ordinates and the distance between "<<endl;
cout<<" each them. "<<endl;
cout<<"(2) Display the above, along with the perimeter and"<<endl;
cout<<" area of the polygon. "<<endl;
cout<<"(3) Display the above, along with the centroid of "<<endl;
cout<<" the polygon. "<<endl;
cout<<"(4) Exit the program. "<<endl;
cout<<"###################################################"<<endl;
cin>>ABCD;

switch (ABCD)
{
case '1':
int corner;
float x[50], y[50], distance[50];
cout << "How many corners are there in your polygon?";
cin >> corner;

if (corner<=2)
{
cout<<"A polygon cannot have less than 3 corners, please try again."<<endl;
}

else
{
coordinates(x, y, corner);
distance(x, y, space, corner);
}
break;

case '2':
int corner;
float x[50], y[50], distance[50], totalA;
cout << "How many corners are there in your polygon?";
cin >> corner;

if (corner<=2)
{
cout<<"A polygon cannot have less than 3 corners, please try again."<<endl;
}

else
{
coordinates(x, y, corner);
distance(x, y, space, corner);
perimeter(space, corner);
area(x, y, corner, totalA);
}
break;

case '3':
int corner;
float x[50], y[50], distance[50], totalA, centx, centy;
cout << "How many corners are there in your polygon?";
cin >> corner;

if (corner<=2)
{
cout<<"A polygon cannot have less than 3 corners, please try again."<<endl;
}

else
{
coordinates(x, y, corner);
distance(x, y, space, corner);
perimeter(space, corner);
area(x, y, corner, totalA);
centroidx(x, y, corner, totalA, centx);
centroidy(x, y, corner, totalA, centy);
}
break;

case '4':
state=2;
break;

default:
cout<<"Error, please choose one of the numbers listed above."<<endl;
break;

}
}
return 0;
}


//#######################################


void coordinates (float x[50], float y[50], int corner)
{

for (int count=0; count<corner; count++)
{
cout<<"What is the x co-ordinate of corner"<<count+1<<"?"<<endl;
cin>>x[count];
cout<<"What is the y co-ordinate of corner"<<count+1<<"?"<<endl;
cin>>y[count];
}
}

void distance (const float x[50], const float y[50], float space[50], int corner);
{
for (int count=0; count<corner-1; count++)
{
space[count]=sqrt(pow(x[count+1]-x[count],2)+pow(y[count+1]-y[count],2));
cout<<"The distance between corner "<<count+1<<" ("<<x[count]<<","<<y[count]<<") "<<"and corner "<<count+2<<" ("<<x[count+1]<<","<<y[count+1]<<") is"<< space[count]<<"."<<endl;
}

space[corner-1]=sqrt(pow(x[corner-1]-x[0],2)+pow(y[corner-1]-y[0],2));
cout<<"The distance between corner "<<corner<<" ("<<x[corner-1]<<","<<y[corner-1]<<") "<<"and the cardinal point"<<" ("<<x[count+1]<<","<<y[count+1]<<") is"<< space[corner-1]<<"."<<endl;
}

void perimeter (const float space[50], int corner)
{
float perimeter=0;
for(int count=0;count<corner;count++)
{
perimeter=perimeter+space[count];

cout<<"The perimeter of the polygon is "<<perimeter<<"."<<endl;
}

void area (const float x[50], const float y[50],int corner, float totalA)
{
float a = 0;
float m;
float n;
for (int count=0; count<corner<-1; count++)
{
m=(x[count]*y[count+1])-(x[count+1]*y[count]);
a=a+m;

}
n=(x[corner-1]*y[0])-(x[0]*y[corner-1]);
totalA=(a+n)/2;

cout<<"The total area of the polygon is "<<totalA<<"."<<endl;
}

void centroidx (const float x[50], const float y[50], int corner, float & totalA, float centx)
{
float m;
float n;
float c=0;
for(int count=0; count<corner-1; count++)
{
m=(x[count]+x[count+1])*((x[count]*y[count+1])-(x[count+1]*y[count]));
c=c+m;
}
n=(x[corner-1]+x[0])*((x[corner-1]*y[0])-(x[0]*y[corner-1]));
centx=(n+m)/(6*totalA);
}

void centroidy (const float x[50], const float y[50], int corner, float & totalA, float centy)
{
float m;
float n;
float c=0;
for (int count=0; count<corner-1; count++)
{
m=(y[count]+y[count+1])*((x[count]*y[count+1])-(x[count+1]*y[count]));
c=c+m;
}
n = (y[corner-1]+y[0])*((x[corner-1]*y[0])-(x[0]*y[corner-1]));
centy=(n+m)/(6*totalA);
cout << "The centroid about the y-axis is " << centr_y << endl;
}
}




The errors I got were the following:

C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(54) : error C2065: 'space' : undeclared identifier
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(59) : error C2086: 'corner' : redefinition
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(60) : error C2086: 'x' : redefinition
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(60) : error C2086: 'y' : redefinition
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(60) : error C2086: 'distance' : redefinition
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(79) : error C2086: 'corner' : redefinition
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(80) : error C2086: 'x' : redefinition
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(80) : error C2086: 'y' : redefinition
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(80) : error C2086: 'distance' : redefinition
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(80) : error C2086: 'totalA' : redefinition
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(130) : error C2447: missing function header (old-style formal list?)
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(152) : error C2601: 'area' : local function definitions are illegal
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(169) : error C2601: 'centroidx' : local function definitions are illegal
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(183) : error C2601: 'centroidy' : local function definitions are illegal

Thanks

BobS0327
December 10th, 2009, 08:35 PM
For starters, address the following issues....

A. C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(54) : error C2065: 'space' : undeclared identifier
space should be defined as a float on line 17
B. C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(59) : error C2086: 'corner' : redefinition
corner is declared in lines 41,59 and 59. comment out these three lines and define corner somewhere near line 17
C. C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(60) : error C2086: 'x' : redefinition
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(60) : error C2086: 'y' : redefinition
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(60) : error C2086: 'distance' : redefinition
The x, y and distance variables are declared in lines 42,60 and 80. comment these variables out in those lines
and declare them near line 17
D. C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(79) : error C2086: 'corner' : redefinition
This is resolved in item B
E. C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(80) : error C2086: 'x' : redefinition
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(80) : error C2086: 'y' : redefinition
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(80) : error C2086: 'distance' : redefinition
The above three are resolved in item C
F. C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(80) : error C2086: 'totalA' : redefinition
totalA is declared in lines 60 and 80. Remove totalA from these two lines and declare totalA around line 17
G. C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(130) : error C2447: missing function header (old-style formal list?)
Remove the semicolon after the end of the function name in line 129
H. C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(152) : error C2601: 'area' : local function definitions are illegal
You've got a function within a function. The area function is within the perimeter function. You must place the area function
outside the perimeter function and add a closing brace to the perimeter function
I. C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(169) : error C2601: 'centroidx' : local function definitions are illegal
C:\Documents and Settings\Administrator\My Documents\Cpp1e.cpp(183) : error C2601: 'centroidy' : local function definitions are illegal
The above two issues should be resolved when you resolve issue H.

1. #include <iostream>
2. #include <cmath>
3. #include <fstream>
4. using namespace std;
5.
6. void coordinates (float x[50], float y[50], int);
7. void distance (const float x[50], const float y[50], float space[50], int);
8. void perimeter (const float space[50], int);
9. void area (const float x[50], const float y[50],int, float);
10. void centroidx (const float x[50], const float y[50], int, float &, float);
11. void centroidy (const float x[50], const float y[50], int, float &, float);
12.
13. int main()
14. {
15. char ABCD;
16. int state=1;
17.
18. while (state==1)
19. {
20. cout<<"###################################################"<<endl;
21. cout<<"###################################################"<<endl;
22. cout<<" Swimming Pool Characteristics "<<endl;
23. cout<<"___________________________________________________"<<endl;
24. cout<<endl;
25. cout<<"Please select one of the options below by selecting"<<endl;
26. cout<<"its corrseponding number. "<<endl;
27. cout<<endl;
28. cout<<"(1) Show the co-ordinates and the distance between "<<endl;
29. cout<<" each them. "<<endl;
30. cout<<"(2) Display the above, along with the perimeter and"<<endl;
31. cout<<" area of the polygon. "<<endl;
32. cout<<"(3) Display the above, along with the centroid of "<<endl;
33. cout<<" the polygon. "<<endl;
34. cout<<"(4) Exit the program. "<<endl;
35. cout<<"###################################################"<<endl;
36. cin>>ABCD;
37.
38. switch (ABCD)
39. {
40. case '1':
41. int corner;
42. float x[50], y[50], distance[50];
43. cout << "How many corners are there in your polygon?";
44. cin >> corner;
45.
46. if (corner<=2)
47. {
48. cout<<"A polygon cannot have less than 3 corners, please try again."<<endl;
49. }
50.
51. else
52. {
53. coordinates(x, y, corner);
54. distance(x, y, space, corner);
55. }
56. break;
57.
58. case '2':
59. int corner;
60. float x[50], y[50], distance[50], totalA;
61. cout << "How many corners are there in your polygon?";
62. cin >> corner;
63.
64. if (corner<=2)
65. {
66. cout<<"A polygon cannot have less than 3 corners, please try again."<<endl;
67. }
68.
69. else
70. {
71. coordinates(x, y, corner);
72. distance(x, y, space, corner);
73. perimeter(space, corner);
74. area(x, y, corner, totalA);
75. }
76. break;
77.
78. case '3':
79. int corner;
80. float x[50], y[50], distance[50], totalA, centx, centy;
81. cout << "How many corners are there in your polygon?";
82. cin >> corner;
83.
84. if (corner<=2)
85. {
86. cout<<"A polygon cannot have less than 3 corners, please try again."<<endl;
87. }
88.
89. else
90. {
91. coordinates(x, y, corner);
92. distance(x, y, space, corner);
93. perimeter(space, corner);
94. area(x, y, corner, totalA);
95. centroidx(x, y, corner, totalA, centx);
96. centroidy(x, y, corner, totalA, centy);
97. }
98. break;
99.
100. case '4':
101. state=2;
102. break;
103.
104. default:
105. cout<<"Error, please choose one of the numbers listed above."<<endl;
106. break;
107.
108. }
109. }
110. return 0;
111. }
112.
113.
114. //#######################################
115.
116.
117. void coordinates (float x[50], float y[50], int corner)
118. {
119.
120. for (int count=0; count<corner; count++)
121. {
122. cout<<"What is the x co-ordinate of corner"<<count+1<<"?"<<endl;
123. cin>>x[count];
124. cout<<"What is the y co-ordinate of corner"<<count+1<<"?"<<endl;
125. cin>>y[count];
126. }
127. }
128.
129. void distance (const float x[50], const float y[50], float space[50], int corner);
130. {
131. for (int count=0; count<corner-1; count++)
132. {
133. space[count]=sqrt(pow(x[count+1]-x[count],2)+pow(y[count+1]-y[count],2));
134. cout<<"The distance between corner "<<count+1<<" ("<<x[count]<<","<<y[count]<<") "<<"and corner "<<count+2<<" ("<<x[count+1]<<","<<y[count+1]<<") is"<< space[count]<<"."<<endl;
135. }
136.
137. space[corner-1]=sqrt(pow(x[corner-1]-x[0],2)+pow(y[corner-1]-y[0],2));
138. cout<<"The distance between corner "<<corner<<" ("<<x[corner-1]<<","<<y[corner-1]<<") "<<"and the cardinal point"<<" ("<<x[count+1]<<","<<y[count+1]<<") is"<< space[corner-1]<<"."<<endl;
139. }
140.
141. void perimeter (const float space[50], int corner)
142. {
143. float perimeter=0;
144. for(int count=0;count<corner;count++)
145. {
146. perimeter=perimeter+space[count];
147.
148. cout<<"The perimeter of the polygon is "<<perimeter<<"."<<endl;
149. }
150.
151. void area (const float x[50], const float y[50],int corner, float totalA)
152. {
153. float a = 0;
154. float m;
155. float n;
156. for (int count=0; count<corner<-1; count++)
157. {
158. m=(x[count]*y[count+1])-(x[count+1]*y[count]);
159. a=a+m;
160.
161. }
162. n=(x[corner-1]*y[0])-(x[0]*y[corner-1]);
163. totalA=(a+n)/2;
164.
165. cout<<"The total area of the polygon is "<<totalA<<"."<<endl;
166. }
167.
168. void centroidx (const float x[50], const float y[50], int corner, float & totalA, float centx)
169. {
170. float m;
171. float n;
172. float c=0;
173. for(int count=0; count<corner-1; count++)
174. {
175. m=(x[count]+x[count+1])*((x[count]*y[count+1])-(x[count+1]*y[count]));
176. c=c+m;
177. }
178. n=(x[corner-1]+x[0])*((x[corner-1]*y[0])-(x[0]*y[corner-1]));
179. centx=(n+m)/(6*totalA);
180. }
181.
182. void centroidy (const float x[50], const float y[50], int corner, float & totalA, float centy)
183. {
184. float m;
185. float n;
186. float c=0;
187. for (int count=0; count<corner-1; count++)
188. {
189. m=(y[count]+y[count+1])*((x[count]*y[count+1])-(x[count+1]*y[count]));
190. c=c+m;
191. }
192. n = (y[corner-1]+y[0])*((x[corner-1]*y[0])-(x[0]*y[corner-1]));
193. centy=(n+m)/(6*totalA);
194. cout << "The centroid about the y-axis is " << centr_y << endl;
195. }
196. }
197.