### Constructive criticism needed for Quadratic equation calculator 3.

Hello.

I need constructive criticism of my quadratic equation calculator (version: 3).

Thank you.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168`` ``````// Quadratic equation calculator. #include #include #include #include double discriminant_function( double func1a, double func1b, double func1c ); double quadratic_formulaplus( double FormulaDiscriminant1, double func2a, double func2b ); double quadratic_formulaminus( double FormulaDiscriminant2, double func3a, double func3b ); int main() { std::string use_again; std::string a_var_s; double a_var = 0; std::string b_var_s; double b_var = 0; std::string c_var_s; double c_var = 0; double discriminant = 0; double result1 = 0; double result2 = 0; std::string correct_equation; std::string correct_equation_validation; do { std::cout << "\n"; std::cout << "Quadratic equation calculator, 2.0.\n"; std::cout << "\n"; std::cout << "This program only calculates quadratic equations that are in ax^2 + bx + c = 0 form.\n"; std::cout << "\n"; std::cout << "This form is also known as standard form.\n"; std::cout << "\n"; while ( correct_equation != "y" ) { std::cout << "Input the value of a:\n"; std::getline ( std::cin, a_var_s ); std::stringstream( a_var_s ) >> a_var; std::cout << "Input the value of b:\n"; std::getline ( std::cin, b_var_s ); std::stringstream( b_var_s ) >> b_var; std::cout << "Input the value of c:\n"; std::getline ( std::cin, c_var_s ); std::stringstream( c_var_s ) >> c_var; std::cout << "\n"; std::cout << "Did you mean to input the following equation: " << a_var << "x^2"; if ( b_var >= 0 ) std::cout << "+" << b_var << "x"; else std::cout << b_var << "x"; if ( c_var >= 0 ) std::cout << "+" << c_var; else std::cout << c_var; std::cout << " = 0?\n"; std::cout << "\n"; do { std::cout << "Input 'y' if the equation was correct.\n"; std::cout << "Input 'n' if the equation was incorrect.\n"; std::cin >> correct_equation; if ( correct_equation != "y" && correct_equation != "n" ) { correct_equation_validation = "n"; std::cout << "Invalid user input. Please try again.\n"; std::cout << "\n"; } else if ( correct_equation == "y" ) correct_equation_validation = "y"; else { correct_equation_validation = "y"; correct_equation = "n"; } } while ( correct_equation_validation == "n" ); } discriminant = discriminant_function( a_var, b_var, c_var ); std::cout << "The discriminant of the equation is " << discriminant << ".\n"; std::cout << "Therefore: "; if ( discriminant >-0.00000000001 && discriminant < 0.00000000001 ) // Never use == to compare floating point variables. { std::cout << "x can only have one real value.\n"; result1 = quadratic_formulaplus( discriminant, a_var, b_var ); std::cout << "x = " << result1; } else if ( discriminant < 0 ) { std::cout << "x has no real values.\n"; } else { std::cout << "x has two real values.\n"; result1 = quadratic_formulaplus( discriminant, a_var, b_var ); std::cout << "x = " << result1 << "\n"; result2 = quadratic_formulaminus( discriminant, a_var, b_var ); std::cout << "OR: x = " << result2 << "\n"; } std::cout << "Input 'y' to use this program again:\n"; std::cin >> use_again; std::cin.ignore(); a_var_s.clear(); b_var_s.clear(); c_var_s.clear(); a_var = 0; b_var = 0; c_var = 0; result1 = 0; result2 = 0; correct_equation.clear(); correct_equation_validation.clear(); } while ( use_again == "y" ); return 0; } double discriminant_function( double func1a, double func1b, double func1c ) { return func1b * func1b - 4 * func1a * func1c; } double quadratic_formulaplus( double FormulaDiscriminant1, double func2a, double func2b ) { double root_var1 = 0; root_var1 = sqrt( FormulaDiscriminant1 ); double dividend1 = 0; dividend1 = -1 * func2b + root_var1; double divisor1 = 0; divisor1 = 2 * func2a; double res1 = 0; res1 = dividend1/divisor1; return res1; } double quadratic_formulaminus( double FormulaDiscriminant2, double func3a, double func3b ) { double root_var2 = 0; root_var2 = sqrt( FormulaDiscriminant2 ); double dividend2 = 0; dividend2 = -1 * func3b - root_var2; double divisor2 = 0; divisor2 = 2 * func3a; double res2 = 0; res2 = dividend2/divisor2; return res2; }``````
I would use separate function which returns a vector of roots, empty vector if there is no roots.

Why do you use stringstream in input? Seems unnecesary overhead here.

corrected_equation_validation and correct_equatiot should be bool and code related to them looks unintuitive and bloated.

Rule of thumb: if bloc of code larger than 40 lines you should conside to divide it into separate functions.
Last edited on
"Why do you use stringstream in input? Seems unnecesary overhead here."

If the user inputs 5a, stringstream will extract the double from the string. So it would equal 5.
Last edited on
You could just use ignore() after each input. Works the same bur without unneeded string >> stream >> double conversions.
Are my functions valid?
They are, but there is many unneeded temporary variables.
For example quadratic_formulaplus can be simplified:
 ``12345`` ``````double quadratic_formulaplus( double FormulaDiscriminant1, double func2a, double func2b ) { double root_var( sqrt(FormulaDiscriminant1) ); return (-func2b + root_var) / (2 * func2a); }``````
I know.
Problem is your code is hard to follow: there is a loop which body is ove 90 lines. All variables declared in the beginning with no regard of their actual scope.
Ideally your main function should look like:
 ``123456789`` ``````int main() { bool use_again; do { quadraticEquation(); use_again = askQuestion("Would you like to use this program again?"); } while ( use_again ); return 0; }``````
Topic archived. No new replies allowed.