| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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; } |
|
#2
|
|||
|
|||
|
Quote:
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. |
|
#3
|
|||
|
|||
|
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. |
|
#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.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > PROBLEM RETURNING Values.. Please HELP! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|