Okay, well, there's many things wrong, here.
1. You can't define functions inside of other functions. main() is a function.
2. Nearly every procedural programming language makes a distinction between
formal parameters and
actual parameters (look it up). I can very well do this:
1 2 3 4 5 6 7
|
int times2(int x){
return 2*x;
}
//...
int foo=5;
int bar=times2(foo);
foo=times2(bar);
|
There doesn't need to be any relation between the expression you pass to a function and the name of the corresponding formal parameter. In fact, most of the time there won't be.
What this means is that the parameter name, as I said before, is not optional.
There is one exception, though: if you don't use the parameter in the function, you can leave it anonymous:
1 2 3
|
int foo(int){
return 2;
}
|
The parameter type is
always mandatory.
3. There are many semicolons where they don't belong. The semicolon terminates statements. If you misplace a semicolon, you can radically change the meaning of a piece of code:
1 2 3 4 5 6 7 8 9 10
|
int foo(int)
{
return 2;
}
[code]Means something very different from
[code]
int foo(int);
{
return 2;
}
|
Depending on the surrounding context, one or the other may compile and the other not. However, you'll practically never want the latter version.
4. In C/++, ^ is not exponentiation.
5. Logic error: The discriminant is b*b - 4*a*c, not (b*b-4)*a*c.
6.
if else
is not a valid sequence of tokens. The valid form is
else if
7. If a function has return type void, the return statement should either not be there, or not be followed by an expression.
1 2 3 4 5 6
|
void foo(){
}
void bar(){
return;
}
|
I'll clean the code up for you, so you can see how it should be done:
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
|
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
// I'll omit the function declarations for stylistic reasons. Feel free to add
// them back if you want.
double getA(){
double aCoeff;
cout << "Enter the a coefficient (non -zero value:\n";
cin >> aCoeff;
while (aCoeff < 0){
cout << "Error: The a-value MUST be non-zero. Try Again:";
cin >> aCoeff;
}
return aCoeff;
}
double calcXvertex(double aCoeff,double bCoeff){
return -bCoeff / ( 2 * aCoeff);
}
double calcYvertex(double aCoeff,double bCoeff,double cCoeff){
double xVertex=calcXvertex(aCoeff,bCoeff);
return aCoeff*xVertex*xVertex + bCoeff*xVertex + cCoeff;
}
void printConcavity(double aCoeff){
if (aCoeff>0)
cout << "Parabola opens UPWARD." << endl;
else
cout << "Parabola opens DOWNWARD." << endl;
}
double calcDiscriminant(double a,double b,double c){
return b*b - 4*a*c;
}
void printRoots(double a,double b,double c){
//The function has to actually be called to take effect:
double discrimitant=calcDiscriminant(a,b,c);
if (discrimitant>0)
cout << "There are two real roots.";
else if (discrimitant==0)
cout << "It has one root with a multiplicity of 2.";
else
cout << "There are no real roots";
}
int main()
{
double aCoeff, bCoeff, cCoeff;
double xVertex, yVertex;
cout << fixed << setprecision(3);
//The function has to actually be called to take effect:
aCoeff=getA();
//TODO: get the other coefficients.
xVertex=calcXvertex(aCoeff,bCoeff);
yVertex=calcYvertex(aCoeff,bCoeff,cCoeff);
//Display the coefficients and vertices
cout << endl << "*******************************************"
<< endl << " Quadratic Equation Analyzer"
<< endl << "*******************************************"
<< endl << "a Coefficient" << setw(30) << aCoeff
<< endl << "b Coefficient" << setw(30) << bCoeff
<< endl << "c Coefficient" << setw(30) << cCoeff
<< endl << "-------------------------------------------"
<< endl << "X Coordinate" << setw(31) << xVertex
<< endl << "Y Coordinate" << setw(31) << yVertex
<< endl << "-------------------------------------------";
//Once again, the function has to be called, or nothing happens.
printConcavity(aCoeff);
cout << endl << "-------------------------------------------";
printRoots(aCoeff,bCoeff,cCoeff);
cout << endl << endl;
return 0;
}
|