left operand l-value

Ok, I need some help. I have done some research on this and nothing has helped. I'm new to this, taking a c programming class. Here is the problem given and what code I have and the errors.

(Algebra: solving linear equations) You can use Cramer’s rule to solve the following 2 x 2 system of linear equation:
a*x+b*y=e, c*x+d*y=f ; x=[(e*d+b*f)/( a*d+b*c)]; y=[(a*f+e*c)/(a*d+b*c)]
Write a program that prompts the user to enter a, b, c, d, e, and f, and display the result. If ad - bc is 0, report that "the equation has no solution."
Test your program by calculating x and y for a = 9.0, b = 4.0, c = 3, d = -5, e = -6, and f = - 21.


/* File name is algebra.cpp
This program will solve a system of equations if the vaule is greater than zero.

Written
*/

#include <iostream>
using namespace std;

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

cout << "Please enter the value of a : " << a;
cout << "\nPlease enter the value of b : " << b;
cout << "\nPlease enter the value of c : " << c;
cout << "\nPlease enter the value of d : " << d;
cout << "\nPlease enter the value of e : " << e;
cout << "\nPlease enter the value of f : " << f;


a*x + b*y= e;
c*x + d*y= f;

x = (e*d - b*f)/(a*d - b*c);
y = (a*f - e*c)/(a*d - b*c);

if (a*d - b*c= 0)
{
cout << "The equation has no solution.";
}


return 0;
}


3 errors:
line 23, 24, 29. "error C2106: '=' : left operand must be l-value


Any help would be great.

dbarto
is this what you're trying to do?
1
2
3
e=a*x + b*y;
f=c*x + d*y;
if (a*d - b*c== 0)


note:

= is for assigning values, lefthand takes the value of the right hand side
http://cplusplus.com/doc/tutorial/operators/

== is for evaluating an expression.
http://cplusplus.com/doc/tutorial/control/
Use code tags: http://cplusplus.com/articles/firedraco1/

In C++, you put a single variable on the left side of the = opreator. This "assigns" or "sets" that variable to whatever the right side is.

IE:

a = 5; makes it so that the variable 'a' contains the number 5.

5 = a; Makes no sense because 5 is not a variable, so you can't assign it to anything.


So if you want to assign 'e' so that it equals a*b + b*y... you'd need to have 'e' on the left side of the assignment operator:

e = a*x + b*y;


Also...

if (a*d - b*c= 0)

Remember that = is assignment, and == is comparison. If you want to check if something is equal to zero, you need to use the == operator, not =.


Also:

cout << "Please enter the value of a : " << a;

This does not get input from the user. Instead it prints the value of the variable a.

If you want to get input from the user, use cin.

EDIT: doh I'm too slow =(
Last edited on
Black and disch, thank you for your help.

Unfortunately I'm still getting an error.

When I put "e" and "f" on the left side, I get more errors (which is why sadly I put them on the right side).

lines: 15-20 & 23(x2)

warning : C4700: uninitialized local variable "a->y" used.

should i redefine the "a->y" ?
Last edited on
Ok, i redefined the variables as "float" and added cin inputs.

That got rid of the errors in the last post but the exact same error now pops up for line 29 (x2).

"unitialized local variable" means what it says. You're never setting x or y to anything. They're uninitialized.

What you're doing is similar to this:

1
2
3
4
int a = 1;  // setting a to 1
int b;   // if you don't set b to anything...

int c = a + b;  // then what do you expect this result to be? 


How can you expect the computer to add a and b together if you never told it what b is? What will the result of that addition be?

You have this problem with x and y in your code. Your program never sets x or y to anything, then it tries to multiply other numbers by x or y. What exactly do you expect the computer to be multiplying with?
Ahhh, i get it. thanks disch.
Topic archived. No new replies allowed.