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...
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.
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.
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.
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.
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.
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 :)))
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.