I'm using DEV C++ to write my programs for my homework in my very first programming class. My professor is rather difficult to understand. I need help!
#include <iostream> #include <math.h> using namespace std; int main() { char ch; double a, x, b, c; x = (-b + sqrt(b^2 - 4*a*c))/(2*a) cout<<" **** Homework 4 - EGR 126-11 ****"<<endl<<endl<<endl; cout<<"solves for x in the equation: a*x^2 + b*x + c = 0."<<endl; cout<<"The user should enter values for a, b, and c (use double data types). " <<endl<<endl; cout<<"Enter a number for 'a'... "<<endl; cin>> a; cout<<"Enter a number for 'b'... "<<endl; cin>> b; cout<<"Enter a number for 'c'... "<<endl; cin>> c; if (a == 0) && (b == 0) cout<<"This condition is 'true'... "<<endl; else cout<<"This condition (a == 0) && (b == 0) is 'false... " <<endl; switch (ch) { case 0: cout<<" a = 0 & b =0, there is no solution for 'x'... "<<endl; break; default: cout<<" 'a' and 'b' should be greater or less than 0... "<<endl; } cout<<" Press 'e' to exit... "<<endl; cin.get(ch); return (0) } *** this is what I currently have and I wanted to see if i can compile and run it but I'm getting so many errors. The main objective here is that I have to solve for x from the quadratic equation a*x^2 + b*x + c = 0. My program must allow users to enter numbers for a, b, and c to solve for 'x'. There are five possible outcomes for x: 1. if a = 0 and b = 0, no solution can be found for x 2. if a = 0 and b != 0, x = -c/b 3. if a != 0 and (b^2 - 4*a*c) = 0, x1 = x2 = -b/(2*a) 4. if a != 0 and (b^2 - 4*a*c) > 0, x1 = (-b + sqrt(b^2 - 4*a*c))/(2*a) x2 = (-b - sqrt(b^2 - 4*a*c))/(2*a) 5. if a != 0 and (b^2 - 4*a*c) < 0, x1 = (-b/(2*a) + (sqrt(4*a*c - b^2)/(2*a)) "i" x2 = (-b/(2*a) - (sqrt(4*a*c - b^2)/(2*a)) "i" where "i" = sqrt(-1); "i" and "+" in text only; real and imaginary parts must be calculated separately. I have to use the if-else code but I don't know where to start and I don't know how to tie things in together.
My errors that i don't know how to fix: x = (-b + sqrt(b^2 - 4*a*c))/(2*a) invalid operands of types `double' and `double' to binary `operator^' if (a == 0) && (b == 0) cout<<"This condition is 'true'... "<<endl; expected identifier before '(' token.
^ in c++ is a bitwise operator http://www.cplusplus.com/doc/tutorial/operators/#bitwise If you are trying to square the number, try and use the pow method http://www.cplusplus.com/reference/cmath/pow/ Also, it might be important to have ((a == 0) && (b == 0)) inside of the same parenthesis. I'm guessing that the parser saw the parenthesis end ')' then came across the unexpected token '&&'. My answers are more or less hunches, since I don't know c++.
@woodrow73 Thank you, putting a and b in the same parentheses actually allowed me to compile and run the program. I've been working on the homework for a while and i'm starting to patch things together i suppose. I hope i get this all figured out. I'm quite confused still but thanks for the help. I'll be checking the links.
Especially when you're confused, break the problem and what you're doing to solve it into smaller pieces. The smaller the easier. BTW, in your code, I didn't see where x or ch were being assigned any value.
Join our real-time social learning platform and learn together with your friends!