Quadratic Formula Solver

Sep 8, 2011 at 11:55pm
Hey guys, i'm having some trouble executing this .cpp file, I would really appreciate some help in finding my error, here's what i've got:

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
double a; double b; double c;
double Posx;
double Negx;
cout<<"Enter the coefficient of the quadratic term> ";
cin>>a;
cout<<"Enter the coefficient of the linear term> ";
cin>>b;
cout<<"Enter the constant term> ";
cin>>c;
cout<<endl;
Posx=(-b+sqrt(b*b)-(4*a*c))/(2*a));
Negx=(-b-sqrt(b*b)-(4*a*c))/(2*a));
cout<<"The solution to the equation "
<<setprecision(5)<<fixed<<float(1)<<a<<"x^2+"
<<setprecision(5)<<fixed<<b<<"x+"
<<setprecision(5)<<fixed<<c<<" = 0.00000 is {"
<<setprecision(5)<<fixed<<Posx<<","
<<setprecision(5)<<fixed<<Negx<<"}."
<<endl;
return 0;
}


cout<<setprecision(5);
cout<<"The solution to the equation "<< fixed <<coefa<<"x^2 +
"<<coefb<<"x + "<<coefc<<" = 0.00000 "<<endl;
cout<<"is {"<<positive equation x<<" , "<<negitive equation x<<"}."<<endl;
return 0;
}
Sep 9, 2011 at 12:27am
Hello, Stokes.

Your issue is caused by the closing brace after the first return statement. Removing that should solve your problem. Also, the second cout statement from the bottom is also causing a problem. Since quotations cannot be spread across multiple lines, the compiler generates a syntax error.
Last edited on Sep 9, 2011 at 12:29am
Sep 9, 2011 at 1:57am
I need to do the same thing. Here is what I have written in Visual C++ Express 2010:

#include "stdafx.h"
#include <iostream>
#include <cmath>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
double quadratic,linear,cons ;
double num1,num2=0;
double power;

cout<<"Enter the coefficient of the quadratic term>";
cin>>quadratic;
cout<<"Enter the coefficient of the linear term>";
cin>>linear;
cout<<"Enter the constant term>";
cin>>cons;

cout << "The solution to the equation" << quadratic << "x^2 +" << linear << "x + " << cons << "= 00.00000";

power=pow (linear / 2 , 2.0);
num1= ( - linear + sqrt(power - ( 4 * quadratic * cons )) ) / (2 * quadratic);
num2= ( - linear - sqrt(power - ( 4 * quadratic * cons )) ) / (2 * quadratic);
cout<<"is {"<<num1<<","<<num2<<"}";

return 0;
}


When I run a debug, I get no errors. However, while running the program, it goes like this:

Enter the coefficient of the quadratic term> *any number*
Enter the coefficient of the linear term> *
Enter the coefficient of the constant term> *

Then the program will shutdown, without displaying an answer...

If you see anything wrong with the code please let me know.


Sep 9, 2011 at 3:28am
Made a few changes.

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
#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <iomanip>
#include <conio.h>
#include <windows.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	double quadratic,linear,cons ;
double num1,num2=0;
double power;

cout<<"Enter the coefficient of the quadratic term>";
cin>>quadratic;
cout<<"Enter the coefficient of the linear term>";
cin>>linear;
cout<<"Enter the constant term>";
cin>>cons;

cout << "The solution to the equation " <<setprecision(5)<<fixed<<quadratic << "x^2 + " <<setprecision(5)<<fixed<<linear<< "x + " <<setprecision(5)<<fixed<<cons<<" = 0.00000\n";

power=pow (linear / 2 , 2.0);
num1= ( - linear + sqrt(power - ( 4 * quadratic * cons )) ) / (2 * quadratic);
num2= ( - linear - sqrt(power - ( 4 * quadratic * cons )) ) / (2 * quadratic);
cout<<"is {"<<setprecision(5)<<fixed<<num1<<","<<setprecision(5)<<fixed<<num2<<"}\n";

cout<<"Press any key to exit.";
_getch();

return 0;

}


So here is the program now:

Enter the coefficient of the quadratic term>4
Enter the coefficient of the linear term>5
Enter the constant term>6
The solution to the equation 4.00000x^2 + 5.00000x + 6.00000 = 0.00000
is {-1.#IND0,-1.#IND0}
Press any key to exit.


What is "#IND0" and what is keeping this from calculating the correct answers? I'd seriously appreciate any help you guys can give me!

Sep 9, 2011 at 9:06am
Hello, coolishmike.

This link may prove useful to you: http://www.johndcook.com/IEEE_exceptions_in_cpp.html
Topic archived. No new replies allowed.