I can't write the line equation y=m*x+b

Hey guys, I recently started to learn c++ and I'm having trouble with writing a simple program to calculate the slope intercept form of a line equation given two points (with x and y coordinates). I was able to get the slope "m", the y intercept "b" but I am unable to get the line equation y=m*x+b. My source code is right below. (When I run it tells me invalid operands of types float and const to binary operator chars[2] for the last line). Thank you guys.



#include <iostream>
using namespace std;

int main()
{
float y1, y2, m, x1, x2, b;
char x, y;


cout<<"Enter first X: ";
cin>> x1;
cout<<"Enter second X: ";
cin>> x2;
cout<<"Enter first Y: ";
cin>> y1;
cout<<"Enter second Y: ";
cin>> y2;

m=(y2-y1)/(x2-x1);
cout<<"The slope is: "<< m <<"\n";
cin.get();

b=y1-(m*x1);
cout<<"The y intercept is: "<< b <<"\n";
cout<<"The line equation is: "<< y=m*x+b <<"\n";

Last edited on
cout<<"The line intercept is: "<< y=m*x+b <<"\n";

You can't cout a variable initialization like this. You need to actually complete the arithmetic and then output the result.
I made a mistake, my last line was
cout<<''The line equation is: " <<y=m*x+b <<"\n";
Thank you for the answer but I do not understand what you are saying, I want my line equation to be on that form y=m*x+b, for example (y=3x+5).
Can anyone give me a little hand?
Then you need to use more << and string literals.

std::cout << "The line equation is: y=(" << m << ")*" << x << "+" << b <<"\n";

If x is also literal move it back inside quotes:
std::cout << "The line equation is: y=(" << m << ")*x+" << b <<"\n";
Last edited on
Yes.

What do you think the statement y=mx+c does in C++ code? It calculates a value, equal to mx+c, and stores that value in y.

Here's a tip to get you started:

cout << "y=";

Oop; Wolfgang beat me to it :)
Last edited on
Thank you guys.
#include <stdio.h>

int main()
{float y1, y2, m, x1, x2, b;
char x, y;

cout<<"enter first x:";
cin>>x1;
cout<<"enter seoncd x:";
cin>>x2;
cout<<"enter first y:";
cin>>y2;

m=(y2-y1)/(x2-x1);
cout<<"the slop is: "<<m<<"\n";
cin.get();

b=y1-(m*x1);
cout<<"the y interceptis: "<<b<<"\n";
cout<<"the line equation is: y=("<<m<<")*"<<x<<"+"<<b<<"\n";

is this what you would end up with?
Topic archived. No new replies allowed.