Cubic equations calculator in C++

Hello everyone!
I'm trying to write a C++ program that calculates the different x values for a cubic equation. I'm building it based on the Ruffini rule. However, when I run it, after I inserted the values, it just stops. Hope you can help me.


#include <iostream>
#include <string>
#include <cmath>
using namespace std;

class func {
public:
int a, b, c, d, a1, b1, c1, r = 0;
bool resolved = false;
int j, k, l = 0;

void get_nums() {
cout << "Use this calculator to solve polynomial equations with an order of 3 such as ax^3 + bx^2 + cx + d = 0 for x including complex solutions.\n\n";

cout << "Insert a: ";
cin >> a;
cout << "Insert b: ";
cin >> b;
cout << "Insert c: ";
cin >> c;
cout << "Insert d: ";
cin >> d;
}

void calculate_func() {
while (!resolved) {
int i = -100;
a1 = a * i;
b1 = b + a1;
c1 = c + i * b1;
r = d + i * c1;

if (r == 0 || i == 100*) {
if (j == 0) {
j = i;
i = i++;
}
else {
if (k == 0){
k = i;
i = i++;
}
else {
l = i;
resolved = true;
}
if (i == 100) {
resolved = true;
}
}
}
else {
i = i++;
}
}
}

void show_results() {
if (j == 0 && k == 0) {
cout << "Your x value doesn't exist.";
}
else if (j == k) {
cout << "Your x value is: " << j;
}
else{
cout << "Your x values are " << j << " and " << k << ".";
}
}
};

int main() {
func Func;
Func.get_nums();
Func.calculate_func();
Func.show_results();
}
What do you mean "it just stops"?
Your program is going to do one of two things.
1. Loop forever in calculate_func().
2. Exit after showing results.
Which is it?

Have you used a debugger to step through your program?

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
A small point but if (r == 0 || i == 100*) contains an error.

Having solved that mystery, for anyone interested in trying to debug this un-commented program, the infinite loop is in the calculate_func()

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

using namespace std;

class func
{
public:
    int a, b, c, d, a1, b1, c1, r = 0;
    bool resolved = false;
    int j, k, l = 0;
    
    void get_nums()
    {
        cout << "Use this calculator to solve polynomial equations blah blah.\n\n";
        
        cout << "Insert a: ";
        cin >> a;
        cout << "Insert b: ";
        cin >> b;
        cout << "Insert c: ";
        cin >> c;
        cout << "Insert d: ";
        cin >> d;
    }
    
    void calculate_func()
    {
        while (!resolved)
        {
            int i = -100;
            a1 = a * i;
            b1 = b + a1;
            c1 = c + i * b1;
            r = d + i * c1;
            
            std::cout << "Got this far\n";
            
            if (r == 0 || i == 100)
            {
                if (j == 0)
                {
                    j = i;
                    i = i++;
                }
                else
                {
                    if (k == 0)
                   {
                        k = i;
                        i = i++;
                    }
                    else
                    {
                        l = i;
                        resolved = true;
                    }
                    if (i == 100)
                    {
                        resolved = true;
                    }
                }
            }
            else
            {
                i = i++;
            }
        }
    }
    
    void show_results() {
        if (j == 0 && k == 0)
        {
            cout << "Your x value doesn't exist.";
        }
        else if (j == k)
        {
            cout << "Your x value is: " << j;
        }
        else
        {
            cout << "Your x values are " << j << " and " << k << ".";
        }
    }
};

int main()
{
    func Func;
    Func.get_nums();
    Func.calculate_func();
    Func.show_results();
}
Last edited on
In member function 'void func::calculate_func()':
45:28: warning: operation on 'i' may be undefined [-Wsequence-point]
52:32: warning: operation on 'i' may be undefined [-Wsequence-point]
67:24: warning: operation on 'i' may be undefined [-Wsequence-point]


Read up on how to increment a variable.

cpp.sh is not showing the warnings about uninitialised variables. Line 12 does not initialise all 3 variables.
cane wrote:
cout << "Use this calculator to solve polynomial equations with an order of 3 such as ax^3 + bx^2 + cx + d = 0 for x including complex solutions.\n\n";


@cane, your code isn't doing anything of the sort.
Topic archived. No new replies allowed.