Hello, I am trying to code a program that can solve quadratic equations, calculate the circumference or surface area, addition, subtraction, multiplication and division. I tried to make the program solve simultaneous equations to find out the answer of x and y. Unfortunately, when the program tries to calculate x and y, it returns an error (-1.#IND) for both x and y. I went through the code numerous times and I can't seem to find a problem with the code. I tested the other 7 modes and they all seem to function perfectly. Can you help me?
To understand what I need help with, try running the code provided, press 8, 2, 3, 3, 2, 130, 170 and look at the two answers. They should be "Your first answer is 10" and "Your second answer is 50" (or the other way round) but they return errors instead.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211`` ``````#include #include #include #include using namespace std; void clearBuffer() { cin.clear(); cin.ignore( numeric_limits< streamsize >::max(), '\n' ); } int radius; double pi = 3.14159265358979323846; double a; double b; double c; double v; double w; double x; double y; double x1; double x2; double x3; double y11 = y * v; double y2 = w * x; double y3 = y11 - y2; double answer1; double answer2; double answer3 = answer1 - answer2; double yfinal = answer3 / y3; double xtimesx = answer1 - (yfinal * y); double finalanswer; double xfinalanswer = xtimesx / x; char choice; double addition (double a, double b) { return (a+b); } double subtraction (double a, double b) { return (a-b); } double multiplication (double a, double b) { return (a*b); } double division (double a, double b) { return (a/b); } double quadratica (double a, double b, double c) { return (-b + sqrt(b*b - 4*a*c))/(2 * a); } double quadraticb (double a, double b, double c) { return (-b - sqrt(b*b - 4*a*c))/(2 * a); } double circle_area_function (double radius) { return (pi*(radius * radius)); } double circumference_function (double radius) { return (2 * pi * radius); } int main(){ cout << "Welcome to the Maths Problem solver, this program has been designed to work out answers for you." << endl; cout << "Pick which kind of formulae do you want to use:" << endl; cout << "1) ax^2 + bx + c = 0" << endl; cout << "2) pi*r^2" << endl; cout << "3) 2 * pi * r" << endl; cout << "4) a+b" << endl; cout << "5) a-b" << endl; cout << "6) a*b" << endl; cout << "7) a/b" << endl; cout << "8) Simultaneous equations" << endl; cin >> choice; { clearBuffer(); } if (choice == '1'){ cout << "Input your a:" << endl; cin >> a; cin.ignore(std::numeric_limits::max(), '\n'); cout << "Input your b:" << endl; cin >> b; cin.ignore(std::numeric_limits::max(), '\n'); cout << "input your c:" << endl; cin >> c; cin.ignore(std::numeric_limits::max(), '\n'); { double x; x = quadratica (a,b,c); cout << "Your first answer is " << x << endl; } { double y; y = quadraticb (a,b,c); cout << "Your second answer is " << y << endl; } } if (choice == '2'){ cout << "Input your radius." << endl; cin >> radius; cin.ignore(std::numeric_limits::max(), '\n'); { double circle_area; circle_area = circle_area_function (radius); cout << "Your answer is " << circle_area << endl; } } if (choice == '3'){ cout << "Insert your radius." << endl; cin >> radius; cin.ignore(std::numeric_limits::max(), '\n'); { double circumference; circumference = circumference_function (radius); cout << "Your answer is " << circumference << endl; } } if (choice == '4'){ cout << "Insert your a." << endl; cin >> a; cout << "Insert your b." << endl; cin >> b; { double addition_answer; addition_answer = addition (a, b); cout << "Your answer is " << addition_answer << endl; } } if (choice == '5'){ cout << "Insert your a." << endl; cin >> a; cout << "Insert your b." << endl; cin >> b; { double subtraction_answer; subtraction_answer = subtraction (a, b); cout << "Your answer is " << subtraction_answer << endl; } } if (choice == '6'){ cout << "Insert your a." << endl; cin >> a; cout << "Insert your b." << endl; cin >> b; { double multiplication_answer; multiplication_answer = multiplication (a, b); cout << "Your answer is " << multiplication_answer << endl; } } if (choice == '7'){ cout << "Insert your a." << endl; cin >> a; cout << "Insert your b." << endl; cin >> b; { double division_answer; division_answer = division (a, b); cout << "Your answer is " << division_answer << endl; } } if (choice == '8'){ cout << "Insert your first x." << endl; cin >> x; cout << "Insert your first y." << endl; cin >> y; cout << "Insert your second x." << endl; cin >> v; cout << "Insert your second y." << endl; cin >> w; cout << "Insert your first answer." << endl; cin >> answer1; cout << "Insert yout second answer." << endl; cin >> answer2; cout << "Your answer is " << xfinalanswer << " and " << yfinal << endl; } system("pause"); }``````

I will appreciate any help.
Thank you!
Last edited on
You have some ambiguity with variable names. Can you look at line 52 and know for sure if the compiler knows that a refers to the parameter a and not the global variable a?
I am not sure what you mean. I thought that if I had (double a, double b) that it related to the stated double a; and double b; (that was the idea). I would like to add, the addition works perfectly, its just the spontaneous equations that is causing the problem. Thanks for you help though!
It looks like you tried to use variables as functions on some lines between 31-45.

When you set a variable equal to something, it doesn't remember the formula on the right-hand side of the equals sign, it calculates the value at runtime instead. So answer3 is being set to equal answer1 - answer2 right when it is created (which will be some random garbage value). The value of answer3 does not change after that point, even if answer1 or answer2 change.

I'm not sure if I explained this well-enough.
Last edited on
You have, thank you! so I am guessing I should add the answer3 = answer1 - answer2 and the rest in the int main() AFTER ive asked them?
That's correct.
Topic archived. No new replies allowed.