Last "else if" statement problem

hello,
I create a simple program with many else if statements which do simple math, my problem is my last else if statement (quadratic) will not execute, also I do not get errors when I compile it.
Any help would be great!
I have been stuck on this for days now, lol.

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
#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>

using namespace std;

int main ()


{
    string op;
    double n1, n2, n3, quotient, sum, difference, product, c, absolute_value, square, x1, x2; 
    
    
    cout<<"Please choose an operation"<<endl;
    cout<<"addition"<<endl;
    cout<<"subtraction"<<endl;
    cout<<"division"<<endl;
    cout<<"multipilcation"<<endl;
    cout<<"fabs"<<endl;
    cout<<"sqrt"<<endl;
    cout<<"quadratic"<<endl;
    cout<<"pythagorean"<<endl;
    cin>>op;
    
    if ((op=="addition") || (op=="subtraction") || (op=="division") || (op=="multipilcation") || (op=="pythagorean") || (op=="fabs")
    || (op=="sqrt") || (op=="quadratic"))
    
            cout<<"Please enter your first number."<<endl;
            cin>>n1;
        
    if ((op=="addition") || (op=="subtraction") || (op=="division") || (op=="multipilcation") || (op=="pythagorean") || (op=="quadratic"))
        {
            cout<<"Please enter your second number:"<<endl;
            cin>>n2;
        }
    if (op=="quadratic")
        {
            cout<<"Please enter your third number:"<<endl;
            cin>>n3;
        }
            
            else if (op=="fabs")
            {
                absolute_value= fabs(n1);
                cout<<"Equation: fabs "<<"("<<n1<<")"<<endl;
                cout<<"Result: "<<absolute_value<<endl;
            }
            
            else if ((op=="sqrt") && (n1<0))
            {
                cout<<"Cannot take square root of negative number."<<endl;
            }
            
           else if (op=="sqrt")
            {
                square=sqrt(n1);
                cout<<"Equation: sqrt( "<<n1<<" )"<<endl;
                cout<<"Result: "<<square<<endl;
            }
            
            else if  (op=="addition")
            {
                sum=n1+n2;
                cout<<"Equation: "<<n1<<" + "<<n2<<endl;
                cout<<"Result: "<<sum<<endl;
            }
        
            else if (op=="subtraction")
            {
                difference=n1-n2;
                cout<<"Equation: "<<n1<<" - "<<n2<<endl;
                cout<<"Result: "<<difference<<endl;
            }
        
            else if ((op=="division") && (n2==0))
            {
                quotient=n1/n2;
                cout<<"Equation: "<<n1<<" / "<<n2<<endl;
                cout<<"Error: Cannot divide by zero."<<endl;
            }
        
            else if (op=="division")
            {
                quotient=n1/n2;
                cout<< "Equation: "<<n1<<" / "<<n2<<endl;
                cout<<"Result: "<<quotient<<endl;
            }
        
            else if (op=="multipilcation")
            {
                product=n1*n2;
                cout<<"Equation: "<<n1<<" * "<<n2<<endl;
                cout<<"Result: "<<product<<endl;
            }
        
            else if (op=="pythagorean")
            {
                c=sqrt(n1*n1+n2*n2);
                cout<<"Equation: c = sqrt( "<<n1<<"^2"<<" + "<<n2<<"^2 )"<<endl;
                cout<<"Result: "<<c<<endl;
            }
        
            else if ((op=="quadratic") && (n3>0))
            {
                x1=(-n2+sqrt(n2*n2-4*n1*n3))/(2*n1);
                x2=(-n2-sqrt(n2*n2-4*n1*n3))/(2*n1);
                cout<<"Equation: "<<n1<<"x^2"<<" + "<<n2<<"x"<<" + "<<n3<<" = "<<" 0 "<<endl;
                cout<<"Cannot take square root of negative number."<<endl;
            }
        
            else if (op=="quadratic")
            {
                x1=(-n2+sqrt(n2*n2-4*n1*n3))/(2*n1);
                x2=(-n2-sqrt(n2*n2-4*n1*n3))/(2*n1);
                cout<<"Equation: "<<n1<<"x^2"<<" + "<<n2<<"x"<<" + "<<n3<<" = "<<" 0 "<<endl;
                cout<<"Result: "<<x1<<" , "<<x2<<endl;
            }
        
            else
            {
                cout<<"Operation not supported"<<endl;
            }
    
    return 0;
}
The first if in the else-if chain test if op=="quadratic" so that's the one that will run.
Thanks Peter!
That makes sense.
How can I make the program skip down to the else if of quadratic?
Why would you need to "skip down"? You already have a conditional block that runs if op is equal to "quadratic". What purpose is there to splitting the code you want to run if that is true into two blocks, one 80 lines below the other?
Last edited on
why do the other math functions skip down but quadratic does not?
Got it thank you Peter and Mikey, you guys saved me!
Topic archived. No new replies allowed.