I wrote a program that solves graphing programs. I am still very new to c++ and mt program is very very rough around the edges but my main problem is when the function returns the value to the main function it outputs random numbers in stead of the correct value. And to make sure that the functions were doing the math correctly I had it output the value before sending it to the main function. here is the code. And again forgive me for its roughness.

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;


// this function is for finding the value of b
float graph (float m, float x2, float y2)
{
	float b;
	
	m = m * x2;
	cout << m;
	system("pause");

	if (m < 0)
	{
		m = m * -1;
		b = m + y2;
	}

	else b = m + y2; 
	cout << m;
	system("pause");
		

	cout << b;
	system("pause");


	return (b);
} 


// this function is for finding the value of m
float slope (float y2, float y1, float x2, float x1)
{
	float m;
	
	// in order to find the slope you must do the formula below
	m = (y2 - y1)/(x2 - x1);
	cout << m;
	system("pause");

	// here i send the values of m, x2 and y2 to find the value of b
	graph (m, x2, y2);

	return(m);
}


int main()
{
	
	float x2, x1, y2, y1;
	int m;
	// i put keep as a cin veraible so that it keeps on running so that i can see my result. dont know how to keep it from automatically shutting down

	cout << "Input y2, y1, x2, x1 in that order and press enter after each input \n";

	//inputs all the values. or coordanints as in "find the slope of (x,y)(x,y) then put it into a x=mx+b form"
	cin >> y2;
	cout << "\n";

	cin >> y1;
	cout << "\n";

	cin >> x2;
	cout << "\n";

	cin >> x1;
	cout << "\n";

	// here i'm sending the values to the slope function
	slope (y2, y1, x2, x1);

	//here i display the formula using the results of slope and graph
	cout << "y=" << slope << "x" << graph << "\n"; 
	// I added this later to see the values of graph and slope because they werent making sense
	cout << graph << "\n";
	cout << slope;

	system("pause");
	
	return 0;
}

Recommended Answers

All 7 Replies

The thing you need to bear in mind is that 'graph' and 'slope' are functions which take parameters and return values, so cout << graph; does not do what you are expecting. I think in this case cout will output the memory location of the function, if it will even compile at all!

Try creating a float variable to store the value returned by the 'slope' function and then cout that.
e.g.

float value = slope(y2,y1,x2,x1);
cout << value;

Or more directly you could do this:

cout << slope(y2,y1,x2,x1) << "\n";

edit: Also you don't seem to be doing anything with the value returned by the call to your 'graph' function, so you'll need to do something similar with that too!

In fact you might want to consider removing the call to 'graph' inside the slope function and call it from main instead.
e.g.

float m = slope(y2,y1,x2,x1);
float c = graph(m,x2,y2);
cout << "y=" << m << "x+" << c << "\n";

what about graph? I want to display its value in main() without sending it any parameters.

what about graph? I want to display its value in main() without sending it any parameters.

graph is a function. It is defined to take arguments. What are you expecting to see by looking at a function without invoking it? The best you are going to get is it's memory address; which is mostly meaningless in your example.

I understand that but it serves its purpose in slope() so all i need it to do is send the value. How do I do that?

Graph doesn't have a value because it is a function.
You seem to be getting confused between variables and functions.
You need to call the function, passing any parameters it requires and use the value it returns.

If you're saying you want to keep the call to the graph function inside the slope function and then have the slope function return both values, then you'd need to return a struct containing both values from the slope function.
e.g.

struct retval{
float m;
float c;
};

retval slope(float y2, float y1, float x1, float x2)
{
    retval ret;
    ret.m = (y2-y1)/(x2-x1);
    ret.c = graph(ret.m, x2, y2);
    return ret;
}

So then in main you could do this:

retval values = slope(y2,y1,x2,x1);
cout << "y=" << values.m << "x+" << values.c << "\n";

All I want is the value of graph() to be returned to main()

Consider graph as a computation. When you say "I want the value of computation" that makes no sense. What you might want is the result of that computation. If that is the case then you need to provide the parameters that are necessary to complete that computation.

Consider the equation n + m . + is the computation and n and m are the arguments to that computation. You wouldn't say "I want the value of + ", would you? You'd more likely say I'd like the results of n + m with appropriate values provided for the arguments n and m .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.