The Program is supposed to repeatedly prompt the user for the three coefficients values,a,b and c, and will then evaluate the expression below and display the two possible values for x(I used X and Y as my two values). And when a=999 it will terminate the program without asking for b or c.
These are the errors I get when I try to compile. Am I even on the right track or am I way off?
Any help would be appreciated
Error E2277 program1.cpp 18: Lvalue required in function main()
Error E2451 program1.cpp 26: Undefined symbol 'Return' in function main()
Error E2379 program1.cpp 26: Statement missing ; in function main()
Error E2134 program1.cpp 27: Compound statement missing } in function main()
*** 4 errors in Compile ***
#include<iostream.h>
#include<math.h>
int main()
{
int X;
int Y;
int a;
int b;
int c;
cin>>a;
while(a!=999)
{
cin>>b;
cin>>c;
}
if (650-7*c*c<=-1)
cout<<"Negative under the radical"<<endl;
elseif (a-4*c=0)
cout<<"Divided by zero"<<endl;
else
{
X=(-3*b)-sqrt(650-7*c*c)/(a-4*c);
Y=(-3*b)-sqrt(650-7*c*c)/(a-4*c);
cout<<X<<endl;
cout<<Y<<endl;
Return 0;
}
Some suggestions:
- Compile your code frequently (continuously, if possible). This will help you catch syntax and logic errors as they appear, avoiding situations where you have to fix code that you don't completely remember.
- Use an editor that indents your code for you (this is basic stuff, but you're certainly working in an environment from 30 years ago). This helps track the structure of your code, and avoids most cases of mismatched braces.
- Use your tools to help you. Compile with as many warnings as your tools support, and fix them.
// NOTE: these headers are obsolete, common in code written before 1998.
// In modern code, the following lines would appear:
// # include <cmath>
// # include <iostream>
#include <math.h>
#include <iostream.h>
int main() {
// NOTE: the user can enter real numbers, not just INTegers. Use "double".
double a, b, c;
cout << "Enter three coefficients of the quadratic equation:\n";
while (cin >> a >> b >> c) {
// NOTE: declare variables as close as possible to where they are used.
// For variables whose values do not need to be modified, use "const".
// NOTE: for terms which appear multiple times, store them in a constant.
doubleconst radicand = 650.0 - (7.0 * c * c);
// NOTE: there is no implicit multiplication
doubleconst divisor = a - 4.0 * c;
if (radicand < 0.0)
cout << "Negative under the radical" << endl;
// NOTE: comparison is ==, assignment is =.
elseif (divisor == 0.0)
cout << "Divided by zero" << endl;
else {
// TODO: rename the constants as something better.
doubleconst minuend = (-3.0 * b);
// NOTE: X and Y have the same value. (Is this intended?)
doubleconst X = minuend - sqrt(radicand) / divisor;
doubleconst Y = minuend - sqrt(radicand) / divisor;
cout << X << '\n' << Y << '\n';
}
cout << "Enter three more coefficients, or control-Z/control-D to quit.\n";
}
return 0;
}
Sorry I wasn't clear on some things.
1. My professor wants us to only use ints or floats in this project as that is what we have learned. So how would I write it without the const.
My professor wants us to only use ints or floats in this project as that is what we have learned. So how would I write it without the const.
Change double to float
The const doesn't change what it is - it just means it's a constant.
If you must not use const too, change doubleconst to float.