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
#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