C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 7th, 2005, 12:29 AM
phobson phobson is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 2 phobson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 44 sec
Reputation Power: 0
PROBLEM RETURNING Values.. Please HELP!

I'm trying to do a program, where i call method, and send a variable to it, then I need to return 2 values from the called method. Here's my code. I guess what i'm trying to find out is if a method can even return 2 separate values. The method is called in order(), and the variable userName is sent to it. Method: getSizeAndIng(), returns 2 variables, and i need something to catch it in order(). Any help would be greatly appreciated.
--Paul
phobson@ucsd.edu

void order()
{
string userName;
int size;
int ing_price;

userName = getUserName();
echoName( userName );

size, ing_price = getSizeAndIng( userName );

calculate( size, ing_price );

cin.ignore();
hitEnter();
return;

}




getSizeAndIng( string& userName )
{
int size;
int ing_price;

cout << "\t" << userName << ", please enter the pizza size (5-25 inches in diameter), SPACE\n"
<< "\tand the price of ingredients (10-15 cents per square inch of pizza),\n"
<< "\tlike 5 10: ";
cin >> size >> ing_price;

echoInfo(userName, size, ing_price);

hitEnter();
return size, ing_price;
}

Reply With Quote
  #2  
Old March 7th, 2005, 12:45 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 275 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 45 m 43 sec
Reputation Power: 4
Quote:
Originally Posted by phobson
void order()
{
string userName;
int size;
int ing_price;

userName = getUserName();
echoName( userName );

size, ing_price = getSizeAndIng( userName );
//I suppose here you want to return two values.
..............
............
}

getSizeAndIng( string& userName )
{
int size;
int ing_price;

cout << "\t" << userName << ", please enter the pizza size (5-25 inches in diameter), SPACE\n"
<< "\tand the price of ingredients (10-15 cents per square inch of pizza),\n"
<< "\tlike 5 10: ";
cin >> size >> ing_price;

echoInfo(userName, size, ing_price);

hitEnter();
return size, ing_price; //Wrong way.

}


For the second bold text I suggest you to let the funciton getSizeAndIng take another parameter and retun it.

Precisely:
int getSizeAndIng( a& , b&); should be the prototype.

Thus parameter 'b' will return size while
return value of function would return the price.

Thus your function should look like,If I have understood your intentions correctly, :-

//initialize size = 0
int getSizeAndIng( string& userName, int& iSize );
{

.....
//calculations or entry of i_size.
cin>>size
//Since iSize is passed as call by ref,any change of value inside will be reflected.
....

return ing_price;
}

Thus , in a way, your funtion returns
ing_price and size simultaneously.

Reply With Quote
  #3  
Old March 7th, 2005, 10:26 AM
Anibal Anibal is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jan 2005
Posts: 176 Anibal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 4 h 20 m 48 sec
Reputation Power: 4
Hey Phobson!

Why not use a simple struct? define a struct with the two values and return that. If you want to do it more oop, you can for example, return an object that contains:

Code:
 
Class blabla{
 
variables: the two necesary values 
methods: 
	- return value 1
	- return value 2
	- asign value 1
	- asign value 2
	- constructor
	- destructor
}

that way, you return an object (the management is yours alone) and simply ask for the variables with the mentioned mehods.

Of course, Cirus'es solution - hi Cirus, how's it going? - is another posibility to go...

Good Luck...

Anibal.

Reply With Quote
  #4  
Old March 7th, 2005, 11:35 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 275 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 45 m 43 sec
Reputation Power: 4
I concur your approach. Good alternative indeed. But Paul was concerned with functions. A third alternative is to use variable argument function.There will be no restrictions on the number.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > PROBLEM RETURNING Values.. Please HELP!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway