3D returning "1" on any calculations

This is my first try at functions and I keep getting the same return of 1.
PLS help.
THX


/* This console apps will calculate the distance between 2 3D obects in 3D space.
This apps will request 2 sets of 3D coor5dinates (x,y,z) from user.
Once all values are recieved the program will call a function that I created to calculated the distance.
The function that I create must pass 6 float values(use const and pass by reference.
It must return a float value.*/

#include<iostream>
#include<cmath>
#include<string>
#include <windows.h>



float const calc_distance(float x1, float x2, float y1, float y2, float z1, float z2)
{
float distance;
distance = sqrt(pow(x2-x1,2)+pow(y2-y1,2)+pow(z2-z1,2));

return distance;
}


using namespace std;

int main(int argc, char *argv[])
{

float x1, x2, y1, y2, z1, z2;

char again = 'y';



SetConsoleTitle("Assignment 7 3D Distance By Shawn Cox "); //name console box

system("color 2b"); // set screen and font color


while ((again == 'y')||(again == 'Y'))
{

//x axis
cout << "\n Enter x1: ";
cin >> x1;
cout << "\n Enter x2: ";
cin >> x2;

//y axis
cout << "\n Enter y1: ";
cin >> y1;

cout << "\n Enter y2: ";
cin >> y2;

//z axis
cout << "\n Enter z1: ";
cin >> z1;

cout << "\n Enter z2: ";
cin >> z2;

// Display
cout << endl;
cout << " Distance = " << calc_distance << endl;

cout << "\n\n Continue? (y = continue, anything = exit): ";
cin >> again;

system ("CLS");

}

return 0;
}
Last edited on
You never actually call calc_distance()
try creating a new var in main
double distance;
then call calc_distance()
distance = calc_distance(x1,x2,y1,y2,z1,z2);
OK I think I have it now, not sure if it is giving right values

#include <iostream>
#include <cmath>
#include <string>
#include <windows.h>

//namespace here before any functions.
using namespace std;

// defined calc_distance function
float const calc_distance(float x1, float x2, float y1, float y2, float z1, float z2)
{
float distance = 0.0;
distance = sqrt(pow(x2-x1,2)+pow(y2-y1,2)+pow(z2-z1,2));

return distance;
}



//start with main()
int main(int argc, char *argv[])
{
SetConsoleTitle("Assignment 7 3D Distance By Shawn Cox "); //name console box
system("color 2b"); // set screen and font color

float x1,x2,y1,y2,z1,z2;

char letter = 'y';

cout << "#########################################################" << endl
<< "# #" << endl
<< "# Determines distance between 2 3D objects #" << endl
<< "# Assignment #7 #" << endl
<< "# By Shawn Cox #" << endl
<< "# #" << endl
<< "#########################################################" << endl;


while ((letter=='y')||(letter=='Y'))
{

//x axis
cout << "\n Enter x1: ";
cin >> x1;
cout << "\n Enter x2: ";
cin >> x2;

//y axis
cout << "\n Enter y1: ";
cin >> y1;

cout << "\n Enter y2: ";
cin >> y2;

//z axis
cout << "\n Enter z1: ";
cin >> z1;

cout << "\n Enter z2: ";
cin >> z2;
cout << endl;

// Call distance function

float const calc_distance(float x1, float x2, float y1, float y2, float z1, float z2);
{
cout << " Distance = " << calc_distance(x1, x2, y1, y2, z1, z2) << endl;

}

cout << "\n\n Continue? (y = continue, anything = exit): ";
cin >> letter;

system ("CLS");

}

return 0;
}
I'm not sure what grcunning was saying... but your first program only had one error: you didn't pass arguments to calc_distance.
cout << " Distance = " << calc_distance << endl; should be cout << " Distance = " << calc_distance(x1, x2, y1, y2, z1, z2) << endl;

Here's why: Variables you declare only exist in the function in which you declare them. You declared all of those variables in main(), but you want to use them in calc_distance(). You already set up calc_distance() to receive all of those, but you didn't pass them.
THX it is clear now!!!
Timaster,
I used that example because my prof has told us to never do any calculations or function calls in a cout statement. He says to always create a new variable, then use the variable in a cout statement.
I'm not sure what his reasoning is, so I just always do it that way
Oh, I see. That is good form, I guess, because it lets you see your function calls more clearly. If they're hidden in a bunch of cout << << << things, it could get confusing.
Sorry if it seems like I ignored your post, but I couldn't understand what you were saying, and I didn't want shawncox to be confused either :)
@grcunning: The reason for this will be that if you have multiple function calls and one of them causes a runtime exception it becomes alot more difficult than if they were on separate lines allocating to variables.
Topic archived. No new replies allowed.