Creating a Quadratic Equation Solver HELP!

Hello, I created this simple program to solve quadratic equations:


#include <iostream>

using namespace std;

int main()
{
cout.setf(ios::fixed, ios::floatfield); cout.precision(8); // sets number of decimal places
double a;
double b;
double c;

cout<< "Welcome to your Quadratic Equation Solving Robot" << endl;
cout<< "First enter your a value in ax^2 ";
cin>> a;
cout<< "Next enter your bx value ";
cin>> b;
cout<< "Finally enter your c value ";
cin>> c;
cout<< "The solution is " << (-b - (b * b - 4 * a * c)^-1) / (2 * a)
<<endl;

return 0;
}

On my final line where it says "cout<< "The solution is " << (-b - (b * b - 4 * a * c)^-1) / (2 * a)" I'm getting an error message that says "invalid operands to binary expression 'double' and 'double'.

Can anyone out there give me a clue as to how to solve this? It seems so straight forward...

Thanks in advance!
im no pro but the "^" sign is not valid in the c++ language. you are to use the cmath library, using the pow function, ie: 2 to the power 4 is written as pow(2,4).
The reason that isn't working is because the ^ isn't used for raising numbers to a power in C++, it is used for the binary XOR command which is something completely different.

You will want to use the pow function from the cmath library for what you are doing.
http://www.cplusplus.com/reference/clibrary/cmath/pow/

So something like this
 
(-b - pow(b * b - 4 * a * c,-1) / (2 * a))


(Not sure if I have my brackets set up correctly for the quadratic equation there, but you get the idea)
Guys/gals, thank you soooo much! Next step will be getting it to display both positive and negative values for the solution. I really appreciate your help.
I just did that code myself, and looks a bit different, well quite a bit.
First don't use double double double..
Hint would be.
int a,b,c,disc;//disc stands for discriminant
float x1,x2;
also include <math.h>// you will need it for sqrt when finding x1,x2.
then remember if discriminant is less then 0, then there is no answer, but if it is equal to zero, then the both x1, x2 is the same, so you should make an if function there.
It was quite a pain in the ass to make it work and also to add loop function to the program, so it asks if you wan't to calculate again,but it is a realy good fealing when it's done. Hope you will make it.

P.S. if you wan't i can give you my version of this script.
Last edited on
Sure, I'd love to take a peak. I'm extremely new to C++ I made this just to play around with manipulating variables. I haven't even gotten to if and else statements, although I've used them in programming Arduinos.
Also I just tried using the pow() function and when I run the program it says build succeeded and gives the name of my debugger but doesn't actually run.

Any more clues?
So this is the final version of my quadratic equation (about 3 weeks of programming experience)






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
#include <iostream>// You can skip the places which are used for the 
#include <math.h>// repeat for now.


using namespace std;
bool Repeat = true;//using for repeat
int main ()
{  

{
    char symbol;//using for repeat
    int a,b,c,i,disc;
    float x1,x2;
    while (Repeat){//also for repeat
    cout << "This programm finds the roots of a quadratic equation" << endl;
    cout << "ax^2 +- bx +- c" << endl;


    cout << "Enter values, a,b and c: " << endl;
    cout << "a: ";
    cin >> a;
    cout << "b: ";
    cin >> b;
    cout << "c: ";
    cin >> c;
    disc = ((b*b) -4*a*c);
    

    
    if (disc > 0)
    {
       cout << "The discriminant is: " << disc << endl;
       x1 = (-b +sqrt((b*b) -4*a*c))/2*a; 
       x2 = (-b -sqrt((b*b) -4*a*c))/2*a;
       }
    else if (disc ==0)
    {
         cout << "The discriminant is: " << disc << endl;
         x1=x2 =(-1*b)/(2*a);

         
    }
    else 
    {
         disc =-1*disc;
    cout << "No answer. Discriminant is less than 0"<< endl;
    cout << "Would you like to calculate again (Y or N): ";//Skip
    cin >> symbol;//Skip
    Repeat = (symbol == 'Y' || symbol == 'y') ? true : false;//And again
    
    }
    cout << "X1: " << x1 << endl;
    cout << "X2: " << x2 << endl; 
    cout << "Would you like to calculate again (Y or N): ";//Skip
    cin >> symbol;//Skip
    Repeat = (symbol == 'Y' || symbol == 'y') ? true : false;//And again
}
    return 0;

}     
}
Last edited on
creating a variable for the discriminant helped, now I can enter in my values for a b and c but now (pretty hilariously) the only answer it gives me is "nan".

Which is good for a laugh but wont help anyone do their math homework.
closed account (G854izwU)
nan stands for not a number. Also I was looking at your code at the very top and I might just be missing this but you don't have the square root part of the quadratic formula. The formula is:

-b +- sqrt(b^2-4*a*b)/2a

Sorry its a bit messy it would look better written out but you get it and the +- is for plus or minus.

-TheNewKid-
Hahahaha oh man big time fail, I was entering in random numbers, apparently it wont solve for complex roots.

Originally instead of using the sqrt math function I was trying to raise it to the power of -1 which causes all sorts of issues.

Also it worked when instead of doing cout << -b +- sqrt(b^2-4*a*b)/2a; i assigned the equation to the variable "disc" like emerican did in his program.

That was fun, as soon as I get to the chapter in my book that deals with if and else statements ill add a repeat and the ability to to give both answers.

My next project will be a simple text based RPG based on Game of Thrones :)))
closed account (G854izwU)
I was just wondering what book you are using? I am also new to C++ and I might want to look into it.
Some parenthesis are missing, you've got to type it in just like a calculator.

2 / 4 * 2 = 1.
2 / (4*2) = .25

This will work more like what you want:

x1 = (-b + sqrt( b*b - ( 4*a*c ))) / (2 * a)

Also:

x ^ -1 != sqrt(x)
x ^ -1 == 1 / (x ^ 1)
sqrt(x) == x ^ (1/2)


Just something I'm curious about, why does c++ even have a sqrt function?
Last edited on
closed account (G854izwU)
Also I hope you guys didn't think the formula I posted above was in C++ code it was just the formula not meant to be used in a C++ program lol.
I'm using "beginning C++ game programming" by Michael Dawson.

I don't particularly care about game programming but this is what was suggested to me by a a local college professor and so far it's good if a little slow.

The formula I used above has been giving me correct answers, I'll write it out and take a look at it again

I had this code on my laptop since my first year comp science. It seems to work well. The advice about computing the discriminant first, testing whether its <, =, or > is the best way to tackle this imo. Take a look.

#include <iostream>
#include <math.h>
#include <conio.h>

using namespace std;

int main ()
{
float temp = 0, root1 = 0, root2 = 0;
float a = 0, b = 0, c = 0;

cout << "Enter the values a, b and c respectively " << endl;
cin >> a >> b >> c;

if ( ((b*b) - (4*a*c)) < 0 ) // tests for negative discriminant
cout << "The roots are imaginary";
else if ( ((b*b) - (4*a*c)) == 0 ) // tests for discriminant equal to zero
cout << "There is only one distinct root: " << -( b / ( 2*a));
else
{
temp = sqrt((b*b) - (4*a*c)); // discriminant is assigned to temp
root1 = ((-b) + temp) / (2*a); // calculate root
root2 = ((-b) - temp) / (2*a); // calculate root
cout << endl << "The roots are: " << endl << root1 << endl << root2;
}
getch();
return 0;
}
Topic archived. No new replies allowed.