Problem with Code!!!:(

I need help figuring out what the problem is with this code for my homework. The question is- if ad-bc is 0, the equation has no solution and is Solvable should be false.

Write a program that prompts the user to enter a,b,c,d,e, and f and displays the result. If ad - bc is 0, report that "The equation has no solution."

My code is right here:

header file "HeaderA.h" :

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

void solveEquation(double a, double b, double c, double d, double e, double f, double &x, double &y, bool &isSolvable)
{
 e = (a * x) + (b * y);
 f = (c * x) + (d * y);
 x = ((e * d) - (b * f)) / ((a * d) - (b * c));
 y = ((a * f) - (e * c)) / ((a * d) - (b * c));
}





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
#include <iostream>
#include "HeaderA.h"
#include <cmath>
using namespace std;

int main ()
{
 double a; double b; double c; double d; double e; double f; double x; double y; bool isSolvable;

 cout << "Enter values for varibles A-F." << endl;
 cin >> a;
 cin >> b;
 cin >> c;
 cin >> d;
 cin >> e;
 cin >> f;

 isSolvable = (a * d) - (b * c);

	if (isSolvable == 0)
	{
	 cout << "Equation has no solution!" << endl;
	}
	else
	{
	 cout << "Your Solution is " << isSolvable << endl;
	}

system("pause");
return 0;
}


One problem that I found is that, no matter what numbers I put in the outcome always comes out to 1.
Thanks for your help!
Where do you use solveEquation() in your code?

Also the solution is (bool)(a * d) - (b * c); as you state in your code, or is something wrong? If so then why you use e,f,x,y if you don't use them?

To solve your question why you always get 1 as a result you have declared isSolvable as bool meaning 0 (false) or 1 (true). Every value different than 0 is considered true thus 1.

Declare isSolvable as double to keep its value and compare its value with 0 to get the correct message. I whould advice to use a syntax like:

if(abs((a * d) - (b * c) < 1e-8)) instead of a comparison with 0 as doubles can be tricky with such comparisons.

1e-8 is just an example you can use any suitable value.

You can also declare variables this way:
double a,b,c,d,e,f,x,y;.
Last edited on
Thanks a lot for your help eypros. I just decided to not use the header file that the book gave me, and that eliminated most of my problems.
Topic archived. No new replies allowed.