int get_value(void); - I put in two function calls because I can't get it to work as single function. I had it working with globals, but I can't get it to work with a local variables.
Below is my program with two functions, please tell me how to rewrite it with one. I won't get any points for it but it will help me to know where I am wrong for the final exam. I have tried it as one function and tried "return (x,y); - when I do this, it returns y but gives a "4" for x. Not sure why it won't work with locals when just "return;" worked with globals.
CODE
#include<iostream>
#include<cmath>
using namespace std;
int get_value_x(void);
int get_value_y(void);
double compute_distance(int x, int y);
int main()
{
double dis;
char ans;
int x, y;
do
{
x = get_value_x();
y = get_value_y();
dis = compute_distance(x, y);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
cout << "The point (" << x << "," << y
<< ") is " << dis << " units from the origin.\n\n";
cout << "Would you like to try another point (Y/N)? ";
cin >> ans;
cout << endl;
}
while ((ans == 'Y') || (ans == 'y'));
return 0;
}
int get_value_x()
{
int x;
cout << "Please enter a nonnegative value for the x-coordinate: ";
cin >> x;
while (x < 0)
{
cout << "The value must be nonnegative, please try "
<< "again.\n";
cout << "Please enter a nonnegative value: ";
cin >> x;
}
return (x);
}
int get_value_y()
{
int y;
cout << "Please enter a nonnegative value for the y-coordinate: ";
cin >> y;
while (y < 0)
{
cout << "The value must be nonnegative, please try "
<< "again.\n";
cout << "Please enter a nonnegative value: ";
cin >> y;
}
return (y);
}
double compute_distance (int x, int y)
{
double dis, z;
z = (x * x) + (y * y);
dis = sqrt(z);
return dis;
}
Mod Edit: added code tags: