7 Replies - 2672 Views - Last Post: 31 October 2008 - 08:22 AM Rate Topic: -----

#1 bdavidson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-September 08

Returning two values through a function? Help me please

Posted 31 October 2008 - 12:23 AM

ok here is my program: My problem is i can't figure out how to return two values through a function. Check out everything in between the comment bars ////////// ////////
but i am trying to get Tbox and TboxR back from the function i made on the bottom and display it from main. ANY help would be great.


#include <iostream>
 
using namespace std;

int TilesPerRoom(int); 
////////////////////////////////
void TotalBox(int, int);
///////////////////////////////
int roomWF;
int roomWI;
int roomLF;
int roomLI;
int Tsize;
int TilesR;
int roomW;
int roomWR;
int RroomW;
int roomL;
int roomLR;
int RroomL;
int Trooms;
int Tbox; 
int TboxR;
int Ttiles = 0;

int main () 
{
	Trooms = 0;
	while (Trooms <1)
	{
		cout << "Enter the number of rooms you need tile for:(Ex:2)";
		cin >> Trooms;
		
		if (Trooms <=0)
		cout << "Please re-enter a positive value of 1 or greater." << endl;
	}
	while (Trooms >=1) 
	{
		Tsize = -1;
		while (Tsize <=0)
	{
		cout << "Enter the size of the tile your using, in inches.(Ex:12)"; 
		cin >> Tsize;
		
		if (Tsize <=0)
		cout << "Please re-enter a positive value greater than 0." << endl;
	}
	roomWF=-1;
	roomWI=-1;
	while (((roomWF ==0) && (roomWI ==0)) || ((roomWF <0) || (roomWI <0)))
	{
		cout << "Enter room width (feet and inches, separated by a space)(Ex:17 4)";
		cin >> roomWF >> roomWI; 
		
		if (((roomWF ==0) && (roomWI ==0)) || ((roomWF <0) || (roomWI <0)))
		cout << "Please re-enter positive values greater than 0." << endl;
	}
	roomLF=-1;
	roomLI=-1;
	while (((roomLF ==0) && (roomLI ==0)) || ((roomLF <0) || (roomLI <0)))
	{
		cout << "Enter room length (feet and inches, separated by a space)(Ex:9 3)";
		cin >> roomLF >> roomLI;
		
		if (((roomLF ==0) && (roomLI ==0)) || ((roomLF <0) || (roomLI <0)))
		cout << "Please re-enter positive values greater than 0." << endl;
	}
	cout << "This room requires " << TilesPerRoom(TilesR) << " tiles" <<endl;
	Trooms--; 

	Ttiles = TilesPerRoom(TilesR) + Ttiles;
	}

	cout << "The total amount of tiles required for the room(s) is " << Ttiles << endl; 


	/////////////////////////////////////////////////////////////////////////////////////
	cout << "You will need " << TotalBox(Tbox) << " boxes for the job." << endl;
	cout << "There will be " << TotalBox(TboxR) << "extra tiles." << endl; 
	/////////////////////////////////////////////////////////////////////////////////////
	
	
	char exit_char;			//Temorary for input
	cout << "\nPress any key and <enter> to exit \n";
	cin >> exit_char;		  //Wait for key response before exiting.

	return 0;

}

int TilesPerRoom(int TilesR) 
{
	roomW = ((roomWF * 12) + roomWI);
	roomWR = roomW % Tsize;
	RroomW = roomW / Tsize;
	
	{
		if (roomWR != 0)
			RroomW++;
	
	}

	roomL = ((roomLF * 12) + roomLI);
	roomLR = roomL % Tsize;
	RroomL = roomL / Tsize;
	
	{
		if (roomLR != 0)
			RroomL++; 
	}

	roomW = RroomW * Tsize;
	roomL = RroomL * Tsize;

	TilesR = ((roomW * roomL)/(Tsize * Tsize));

	return TilesR; 
} 
///////////////////////////////////////////////////////////////////////
void TotalBox(int Tbox, int TboxR) 
{
	TboxR = (Ttiles % 20);
	Tbox = (Ttiles / 20);
	{
		if (TboxR !=0)
			Tbox++;
	}
	return;
}
////////////////////////////////////////////////////////////////////////////

This post has been edited by bdavidson: 31 October 2008 - 01:28 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Returning two values through a function? Help me please

#2 GravityGuy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-January 08

Re: Returning two values through a function? Help me please

Posted 31 October 2008 - 01:44 AM

All you have to do is use pass-by-reference instead of pass-by-value. So the new function definition would be:
void TotalBox(int &Tbox, int &TboxR)



Nothing changes up top where you make the function call because the variable name is the address. Correction, the function prototype right at the top needs adjusting:
void TotalBox(int&, int&);   // pass-by-reference parameters



You don't really need a return statement in a void function.

This post has been edited by GravityGuy: 31 October 2008 - 01:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 bdavidson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-September 08

Re: Returning two values through a function? Help me please

Posted 31 October 2008 - 02:11 AM

ok i changed it but now the compiler just says error C2660: 'TotalBox' : function does not take 1 arguments
for both when i try to display them out of main.

#include <iostream>
#include <iomanip> 
 
using namespace std;

int TilesPerRoom(int); 
////////////////////////////////
void TotalBox(int&, int&);
///////////////////////////////
int roomWF; //Room width in feet entered by user
int roomWI;	//Room width in inches entered by user
int roomLF;	//Room length in feet entered by user
int roomLI; //Room length in inches entered by user
int Tsize;  //Tile size entered by user
int TilesR; //Tiles remaining
int roomW;	//Room total width in inches
int roomWR; //Room width remainder of Total inches divided by Tile size
int RroomW; //Room width in feet rounded up
int roomL;  //Room total length in inches
int roomLR;	//Room length remainder of Total inches divided by Tile size
int RroomL;	//Room length in feet rounded up
int Trooms;	//Total rooms enered by user
int Tbox;	//Total boxes needed
int TboxR;	//Total tiles remaining
int Ttiles = 0;	//Temporary value used until loop iterates

int main () 
{
	//Tells the user what the program is doing for them 
	cout << setprecision(60) << "This Program will estimates the number of boxes of tile " 
		 << endl
		 << setprecision(60) <<	"that you will be needing for a job. Just enter the number "
		 << endl
		 << setprecision(60) << "of rooms and the dimensions of each room and then the "
		 << endl
		 << setprecision(60) << "program will estimate and calculate the total number of "
		 << endl
		 << setprecision(60) << "tiles you will need, the numbers of boxes needed and the "
		 << endl
		 << setprecision(60) << "amount of tiles that will be remaining." << endl << endl;

	Trooms = 0;		   //Allows entrance to the loop below 
	while (Trooms <1)  //Loop for repition of question below if the 
					   //number of rooms entered by the user is invalid
	{
		cout << "Enter the number of rooms you need tile for:(Ex:2)";
		cin >> Trooms; //Gets number of rooms from user
		
		if (Trooms <1) //Checks if what the user entered was less than 1
		cout << "Please re-enter a positive value of 1 or greater." << endl;
	}
	while (Trooms >=1) //Loop that allows you to find the total tiles needed for more than 1 room
	{
		Tsize = -1;		  //Allows entrance into the loop below
		while (Tsize <=0) //Loop used for repition of question below if the 
						  //number of rooms entered by the user is invalid 
	{
		cout << "Enter the size of the tile your using, in inches.(Ex:12)"; 
		cin >> Tsize; //Gets Tile size from user
		
		if (Tsize <=0) //Checks if what the user entered is less than or equal to 0
		cout << "Please re-enter a positive value greater than 0." << endl;
	}
	roomWF=-1; //Allows entrance into the loop below
	roomWI=-1; //Allows entrance into the loop below
	//Loop used for repition of the question below if the data the user entered is invalid
	while (((roomWF ==0) && (roomWI ==0)) || ((roomWF <0) || (roomWI <0)))
	{
		cout << "Enter room width (feet and inches, separated by a space)(Ex:17 4)";
		cin >> roomWF >> roomWI; //Gets room width in feet and inches from user
	//Checks if what the user entered was incorrect and then tells them to re-enter
		if (((roomWF ==0) && (roomWI ==0)) || ((roomWF <0) || (roomWI <0)))
		cout << "Please re-enter positive values greater than 0." << endl;
	}
	roomLF=-1; //Allows entrance into the loop below
	roomLI=-1; //Allows entrance into the loop below
	//Loop used for repition of the question below if the data the user entered is invalid
	while (((roomLF ==0) && (roomLI ==0)) || ((roomLF <0) || (roomLI <0)))
	{
		cout << "Enter room length (feet and inches, separated by a space)(Ex:9 3)";
		cin >> roomLF >> roomLI; //Gets room length in feet and inches from user
	//Checks if what the user entered was incorrect and then tells them to re-enter
		if (((roomLF ==0) && (roomLI ==0)) || ((roomLF <0) || (roomLI <0)))
		cout << "Please re-enter positive values greater than 0." << endl;
	}
	cout << "This room requires " << TilesPerRoom(TilesR) << " tiles" <<endl;
	Trooms--; 
	
 
	Ttiles = TilesPerRoom(TilesR) + Ttiles; //Gets the total Tiles for the job
	}

	cout << "The total amount of tiles required for the room(s) is " << Ttiles << endl; 
	
	
	/////////////////////////////////////////////////////////////////////////////////////
	cout << "You will need " << TotalBox(Tbox) << " boxes for the job." << endl;
	cout << "There will be " << TotalBox(TboxR) << "extra tiles." << endl; 
	/////////////////////////////////////////////////////////////////////////////////////


	
	
	char exit_char;			//Temorary for input
	cout << "\nPress any key and <enter> to exit \n";
	cin >> exit_char;		  //Wait for key response before exiting.

	return 0;

}

int TilesPerRoom(int TilesR) 
{
	roomW = ((roomWF * 12) + roomWI); //Calculates total width in inches
	roomWR = roomW % Tsize;	//Calculates Room width remainder of Total inches divided by Tile size
	RroomW = roomW / Tsize; //Calculates the rooms width
	
	{ //If no remainder rounds up the rooms width by adding 1
		if (roomWR != 0)
			RroomW++;
	
	}

	roomL = ((roomLF * 12) + roomLI); //Calculates total length in inches
	roomLR = roomL % Tsize; //Calculates Room length remainder of Total inches divided by Tile size
	RroomL = roomL / Tsize; //Calculates the rooms length
	
	{ //If no remainder rounds up the rooms width by adding 1
		if (roomLR != 0)
			RroomL++; 
	}

	roomW = RroomW * Tsize; //Reassign rounded up value of total width in inches
	roomL = RroomL * Tsize; //Reassign rounded up value of total length in inches

	TilesR = ((roomW * roomL)/(Tsize * Tsize)); //Calculates total amount of tiles 

	return TilesR; //returns value to main
} 
///////////////////////////////////////////////////////////////////////
void TotalBox(int &Tbox, int &TboxR) 
{
	TboxR = (Ttiles % 20);
	Tbox = (Ttiles / 20);
	{
		if (TboxR !=0)
			Tbox++;

	}

}
//////////////////////////////////////////////////////////////////////////// 

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: Returning two values through a function? Help me please

Posted 31 October 2008 - 02:36 AM

View Postbdavidson, on 31 Oct, 2008 - 05:11 AM, said:

ok i changed it but now the compiler just says error C2660: 'TotalBox' : function does not take 1 arguments

You have to change everytime that you either reference the function, or pass data to it.

Classic "Do as I want not as I say"
Was This Post Helpful? 0
  • +
  • -

#5 bdavidson  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 15-September 08

Re: Returning two values through a function? Help me please

Posted 31 October 2008 - 02:41 AM

i'm sorry i'm not sure i know what you mean by that? Any chance you can show me or?
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,916
  • Joined: 10-May 07

Re: Returning two values through a function? Help me please

Posted 31 October 2008 - 03:04 AM

#include <iostream>
#include <iomanip> 
 
using namespace std;

int TilesPerRoom(int); 
////////////////////////////////
void TotalBox(int&, int&);  // <----------You declare the function to take two arguments
///////////////////////////////
...
int main () 
{
...
	/////////////////////////////////////////////////////////////////////////////////////
	cout << "You will need " << TotalBox(Tbox) << " boxes for the job." << endl;
	cout << "There will be " << TotalBox(TboxR) << "extra tiles." << endl; 
	/////////////////////////////////////////////////////////////////////////////////////
// However, as you can see here, you only pass one argument into the function.



When you declare the function, you declare it type void, so it will have no return value, & you declare it to take argument 1, int, argument 2, int. However, when you actually call the function you only pass TboxR into it. It needs another argument to be fed in, or at least that's how you've declared it.

Does this makes sense?

Here is a another quick example :

#include <stdio.h>

int show(int a, int b);

int main(void) {
  int i=0,num1=1,num2=2;
  i=show(num1,num2);
  printf("the result of the function is %d if given %d and %d\n\n",i,num1,num2);

  return 0;
}

int show(int a, int b) {
  return (a + b);
}


Was This Post Helpful? 0
  • +
  • -

#7 RedSonja  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 9
  • View blog
  • Posts: 172
  • Joined: 04-September 08

Re: Returning two values through a function? Help me please

Posted 31 October 2008 - 04:56 AM

cout << "You will need " << TotalBox(Tbox) << " boxes for the job." << endl;

TotalBox returns void so that won't get you far. Also, TotalBox expects 2 arguments, not one. So output Tbox itself after you have called TotalBox:

TotalBox(&Tbox, &otherthing)
cout << "You will need " << Tbox << " boxes for the job." << endl;

********************************************************

if you want Tbox to come back modified, you have to declare it like this:

TotalBox(int *Tbox, int *otherthing);

and call it like this:

TotalBox(&Tbox, &otherthing)
Was This Post Helpful? 0
  • +
  • -

#8 GravityGuy  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 48
  • Joined: 21-January 08

Re: Returning two values through a function? Help me please

Posted 31 October 2008 - 08:22 AM

Ok, now everyone is confusing you. You had it right but you didn't carry through to the end. After calling the function to change the variables, just use the variables in the cout statement. Recap:
function prototype at the top: void TotalBox(int&, int&);
function definition: void TotalBox(int & tb, int &tbr) { // change tb and tbr }
function use: TotalBox(Tbox, TboxR);
printing use: cout << "You wil need " << TBox << " boxes for the job, ";
and more: cout << "and " << TboxR << " extra tiles." << endl;

I would go with references here, that's the &, not pointers, that's the *. The reason is that you aren't doing any pointer incrementing through an array or any other need to modify the memory address, in fact, you could use const int& to make sure you don't change the memory address of a variable, just the value stored there. Look at the classic scanf C function usage: scanf("s", &stringVar); as an example.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1