Constructive criticism needed for Quadratic equation calculator 3.

Hello.

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

Thank you.

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Quadratic equation calculator.
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>

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:
1
2
3
4
5
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:
1
2
3
4
5
6
7
8
9
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.