I am trying to write this "program" for my distance ed course and I almost have it done I just can't figure out how to get the values from the function BoxDimensions into the function DisplayData so that it can print out the dimensions. Help????
Thanks.

#include <iostream>
using namespace std;

double BoxDimensions (double a, double b, double c, double d)
{
	
	
	if (a==0)
	{a=d/b/c;
	return a;}

	else if (b==0)
	{b=d/a/c;
	return b;}

	else if (c==0)
	{c=d/a/b;
	return c;}

	else if (d==0)
	{d=a*b*c;
	return d;}

	return 0;
	cout<<endl;
}
double DisplayData (double f, double g, double h, double i)
	{
	
	double e;

	cout<< "Length: "<<f<<endl;

	cout<< "Width: "<<g<<endl;
	
	cout<< "Height: "<<h<<endl;
	
	cout<< "Volume: "<<i<<endl;
	e=f*g;
	cout<<"Area: "<<e<<endl;
   return 0;
   cout<<endl;
}
int ProcessAnother()
{
		char again= 'y';
		for (again=again; (again=='y' ||again=='Y'); again=again)
		{
			cout<<"Would you like to run this program again (y or n)?"<<endl;
			cin>>again;
			cout<<endl;
			
			if (again=='n' || again =='N')
			{
				again='n';
			}
			
			if (again=='y' || again=='Y')
			{
				double l,w,h,v;
				cout<<"Please enter dimensions. For the dimension you wish to find please enter a 0. "<<endl;
				cout<<"Length: ";
				cin>> l;
				cout<<"Width: ";
				cin>> w; 
				cout<<"Height: ";
				cin>> h; 
				cout<<"Volume: ";
				cin>> v;
				cout<<endl<<endl;
				cout<<BoxDimensions(l,w,h,v)<<endl<<endl;
				cout<<DisplayData(l,w,h,v)<<endl;
				cout<<ProcessAnother()<<endl;
			}
		}
		return 0;
		cout<<endl;
}
int main()
{
	double l,w,h,v;
	 cout<<"Please enter dimensions. For the dimension you wish to find please enter a 0. Only enter one 0 per calculation. "<<endl;
	cout<<"Length: ";
	cin>> l;
	cout<<"Width: ";
	cin>> w; 
	cout<<"Height: ";
	cin>> h; 
	cout<<"Volume: ";
	cin>> v;
cout<<endl<<endl;
	cout<<BoxDimensions(l,w,h,v)<<endl<<endl;
	cout<<DisplayData(l,w,h,v)<<endl;
	cout<<ProcessAnother()<<endl;
	
}

Recommended Answers

All 5 Replies

First of all: Thank you for using code-tags in your very first post! Good job.

Now for your problem:
How about sending the data by reference?

Here's an example of passing a value by reference:

#include <iostream>

using namespace std;

void timestwo(double* num)
{
  *num*=2;
  return;
}

void  addone(double *num)
{
	*num+=1;
	  return;
}
int main()
{
	double number = 2.1;
	cout << "number = " << number << endl;
	timestwo(&number);
	cout << "number = " << number << endl;
	addone(&number);
	cout << "number = " << number << endl;
	cin.get();
        return 0;
}

You could also store all the values in a struct and return an entire structs from the function

hi
try this in main():

double volumn;
cout<<(volumn = BoxDimensions(l,w,h,v))<<endl<<endl;
cout<<DisplayData(l,w,h,volumn)<<endl;

btw, you should better "sort" the parameters of both functions.

krs,
tesu

thanks Tesu but I'm not sure that's going to work. I tried it...but I need to program to be able to return the values from whichever if statement it is because the user has to be able to enter 0 into either length, width, height or volume...not just volume.

Ok, I know whe're not supposed to give away code, but since you've made a serious effort to do it yourself AND you used code-tags in your first post:

I've modded your program to work by reference. It's not done, nor great, but that's for you to figure out ;)

#include <iostream>
using namespace std;

void BoxDimensions (double* a, double *b, double *c, double* d)
{
	if (*a==0) *a=(*d)/(*b)/(*c);

	else if (*b==0) *b=(*d)/(*a)/(*c);

	else if (*c==0)*c=(*d)/(*a)/(*b);

	else if (*d==0)(*d)=(*a)*(*b)*(*c);
}
void DisplayData (double *f, double* g, double* h, double *i)
{
	double e;
	cout << "\n\n";

	cout<< "Length: "<<*f<<endl;

	cout<< "Width: "<<*g<<endl;

	cout<< "Height: "<<*h<<endl;

	cout<< "Volume: "<<*i<<endl;
	e=(*f)*(*g);
	cout<<"Area: "<<e<<endl;
}


int main()
{
	double l,w,h,v;
	cout<<"Please enter dimensions. For the dimension you wish to find please enter a 0. Only enter one 0 per calculation. "<<endl;
	cout<<"Length: ";
	cin>> l;
	cout<<"Width: ";
	cin>> w; 
	cout<<"Height: ";
	cin>> h; 
	cout<<"Volume: ";
	cin>> v;

	BoxDimensions(&l,&w,&h,&v);
	DisplayData(&l,&w,&h,&v);
	cin.ignore();
	cin.get();
}

But personally, I would start over and use structs or even classes. This requires a lot less 'pointer throwing'

thankyou so much! Iv been working forever on this and I just couldn't figure it out...the joys of distance ed :S...you're learning from textbooks that seem to skip over the important stuff. lol
once again thankyou!

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.