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";
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.
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.
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.