Reputation: 15
my intro computer science class just covered functions this week and after checking the major thread for "Unresolved external symbols" I think it is either
a) You declared the functions but never called them after main
b) You are missing the correct library
I'm just not sure which one it is or how to correctly go about it. Also, I think my logic is slightly flawed in the calcSideC block although Im not sure
#include <iostream>
#include <cmath>
using namespace std;
float getSide();
float calcSideC(float sideA, float sideB, float total);
void displaySideC(float sideC);
int main()
{
{
float sideA = 0.0;
float sideB = 0.0;
float total = sideA + sideB;
float sideC = sqrt(total);
sideA = getSide();
sideB = getSide();
sideC = calcSideC(sideA, sideB, total);
displaySideC(sideC);
return 0;
}
float getSide();
{
float sideA;
cout << "Enter two sides of a right triangle.\n\n" << "Side A: \n" << "Please enter the dimension: ";
cin >> sideA;
return sideA;
}
float getSide();
{
float sideB;
cout << "\n\n" << "Side B: \n" << "Please enter the dimension: ";
cin >> sideB;
return sideB;
}
float calcSideC(float sideA, float sideB, float total);
{
float sideA;
float sideB;
float total;
float sideC;
pow(sideA, 2);
pow(sideB, 2);
float sqrt(total);
return sideC;
}
void displaySideC(float sideC);
{
float sideC;
cout << "The dimension of Side C is: " << sideC;
}
system("pause");
return 0;
}
Upvotes: 0
Views: 182
Reputation: 597166
You can't define functions inside of other functions (not that you are doing it correctly anyway, as you have extra ;
that are causing the code to behave differently than you are expecting).
You need to move the function definitions out of main()
.
Now, with that syntax error fixed, you still have quite a few logic errors in your code, including:
declaring function local variables that are the same names as function parameters
ignoring the return values of std::pow()
an std::sqrt()
calculating total
before sideA
and sideB
have been assigned values by the user.
Try something more like this instead:
#include <iostream>
#include <cmath>
using namespace std;
float getSideA()
{
float sideA;
cout << "\n\n" << "Side A: \n" << "Please enter the dimension: ";
cin >> sideA;
return sideA;
}
float getSideB()
{
float sideB;
cout << "\n\n" << "Side B: \n" << "Please enter the dimension: ";
cin >> sideB;
return sideB;
}
float calcSideC(float sideA, float sideB)
{
return sqrt(pow(sideA, 2) + pow(sideB, 2));
}
void displaySideC(float sideC)
{
cout << "The dimension of Side C is: " << sideC;
}
int main()
{
cout << "Enter two sides of a right triangle.";
float sideA = getSideA();
float sideB = getSideB();
float sideC = calcSideC(sideA, sideB);
displaySideC(sideC);
system("pause");
return 0;
}
Or, if you want to forward-declare the functions:
#include <iostream>
#include <cmath>
using namespace std;
float getSideA();
float getSideB();
float calcSideC(float sideA, float sideB);
void displaySideC(float sideC);
int main()
{
cout << "Enter two sides of a right triangle.";
float sideA = getSideA();
float sideB = getSideB();
float sideC = calcSideC(sideA, sideB);
displaySideC(sideC);
system("pause");
return 0;
}
float getSideA()
{
float sideA;
cout << "\n\n" << "Side A: \n" << "Please enter the dimension: ";
cin >> sideA;
return sideA;
}
float getSideB()
{
float sideB;
cout << "\n\n" << "Side B: \n" << "Please enter the dimension: ";
cin >> sideB;
return sideB;
}
float calcSideC(float sideA, float sideB)
{
return sqrt(pow(sideA, 2) + pow(sideB, 2));
}
void displaySideC(float sideC)
{
cout << "The dimension of Side C is: " << sideC;
}
Upvotes: 2
Reputation: 23485
You declared your functions inside main! This is wrong. Move them outside. Next, functions do NOT have semi-colons on the declarative line..
IE: void func(); {}
is wrong because it should be void func() {}
with no semi-colon.
getSide()
TWICE..system("pause");
calcSideC
has local variables which are same as the parameters (redeclaration error).displaySideC
has local variable which is the same as the parameters (redeclaration error).calcSideC
doesn't use the result of sqrt(total);
.calcSizeC
doesn't actually calculate sideC using pythagorean theorem because you don't even use pow
result..Correct code would be:
#include <iostream>
#include <cmath>
using namespace std;
float getSideA();
float getSideB();
float calcSideC(float sideA, float sideB);
void displaySideC(float sideC);
int main()
{
float sideA = 0.0;
float sideB = 0.0;
float sideC = 0.0;
sideA = getSideA();
sideB = getSideB();
sideC = calcSideC(sideA, sideB);
displaySideC(sideC);
system("pause");
return 0;
}
float getSideA()
{
float sideA;
cout << "Enter two sides of a right triangle.\n\n" << "Side A: \n" << "Please enter the dimension: ";
cin >> sideA;
return sideA;
}
float getSideB()
{
float sideB;
cout << "\n\n" << "Side B: \n" << "Please enter the dimension: ";
cin >> sideB;
return sideB;
}
float calcSideC(float sideA, float sideB)
{
float total = pow(sideA, 2);
total = total + pow(sideB, 2);
return sqrt(total);
}
void displaySideC(float sideC)
{
cout << "The dimension of Side C is: " << sideC;
}
Upvotes: 1