### Need constructive criticism for Quadratic equation calculator 2.0!

Pages: 12
Better indented:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131`` ``````#include #include #include #include using namespace std; int main() { string use_again; string a_var_s; long double a_var = 0; string b_var_s; long double b_var = 0; string c_var_s; long double c_var = 0; long double b_var_sqrd = 0; long double discriminant = 0; unsigned int discriminant_indication = 0; long double divisor = 0; long double dividend = 0; long double root_var = 0; long double res = 0; long double res_2 = 0; string correct_equation; string correct_equation_validation; do { cout << endl; cout << "Quadratic equation calculator, 2.0." << endl; cout << endl; cout << "This program only calculates quadratic equations that are in ax^2 + bx + c = 0 form." << endl; cout << endl; cout << "This form is also known as standard form." << endl; cout << endl; while ( correct_equation != "y" ) { cout << "Input the value of a:" << endl; getline ( cin, a_var_s ); stringstream( a_var_s ) >> a_var; cout << "Input the value of b:" << endl; getline ( cin, b_var_s ); stringstream( b_var_s ) >> b_var; cout << "Input the value of c:" << endl; getline ( cin, c_var_s ); stringstream( c_var_s ) >> c_var; cout << endl; cout << "Did you mean to input the following equation: " << a_var << "x^2"; if ( b_var >= 0 ) cout << "+" << b_var << "x"; else cout << b_var << "x"; if ( c_var >= 0 ) cout << "+" << c_var; else cout << c_var; cout << " = 0?" << endl; cout << endl; do { cout << "Input 'y' if the equation was correct." << endl; cout << "Input 'n' if the equation was incorrect." << endl; cin >> correct_equation; if ( correct_equation != "y" && correct_equation != "n" ) { correct_equation_validation = "n"; cout << "Invalid user input. Please try again." << endl; cout << endl; } else if ( correct_equation == "y" ) correct_equation_validation = "y"; else { correct_equation_validation = "y"; correct_equation = "n"; } cin.ignore(); } while ( correct_equation_validation == "n" ); } b_var_sqrd = pow ( b_var, 2 ); discriminant = b_var_sqrd - 4*a_var*c_var; cout << "The discriminant of the equation is " << discriminant << "." << endl; cout << "Therefore: "; if ( discriminant >-0.00000000001 && discriminant < 0.00000000001 ) // Never use == to compare floating point variables. { cout << "x can only have one real value." << endl; discriminant_indication = 1; } else if ( discriminant < 0 ) { cout << "x has no real values." << endl; discriminant_indication = 0; } else { cout << "x has two real values." << endl; discriminant_indication = 2; } root_var = sqrt( discriminant ); dividend = -1 * b_var + root_var; divisor = 2 * a_var; res = dividend/divisor; if ( discriminant_indication == 1 ) cout << "x = " << res << "." << endl; else if ( discriminant_indication == 2 ) { cout << "x = " << res << "." << endl; dividend = -1 * b_var - root_var; divisor = 2 * a_var; res_2 = dividend/divisor; cout << "OR: x = " << res_2 << "." << endl; } cout << "Input 'y' to use this program again:" << endl; cin >> use_again; correct_equation = "n"; cin.ignore(); } while ( use_again == "y" || use_again == "Y" ); cin.get(); return 0; }``````
Topic archived. No new replies allowed.
Pages: 12