0

Possible Duplicate:
Returning multiple values from a C++ function

/************************************************/
/* Name: premserv                               */
/* Description: Calculations for premium serv   */
/* Parameters: N/A                              */
/* Return Value: premserv                       */
/************************************************/

float premserv ()
{
  int daymin = 0;         //Var for day minutes
  int nightmin = 0;       //Var for night minutes
  float daytotal = 0;     //Var for day total
  float nighttotal = 0;   //Var for night total
  float premserv = 0;     //Var for premium service cost

  cout << "\n" << "Please enter the number of minutes used durring the day (6AM - 6PM): " << "\n";
  cin >> daymin;
  cout << "\n" << "Please enter the number of minutes used durring the night (6PM - 6AM): " << "\n";
  cin >> nightmin;

  daytotal = (daymin - 75) * 0.1;
  nighttotal = (nightmin - 100) * 0.05;
  premserv = 25 + daytotal + nighttotal;

  return premserv;

}

I need to get the values from daymin, nightmin, and premserv out of the function. I only know how to get 1 value out of a function.

Community
  • 1
  • 1
Sam LaManna
  • 425
  • 2
  • 7
  • 15

6 Answers6

8

Create a struct with all three floats and return that instead of the single float.

plackemacher
  • 4,216
  • 1
  • 23
  • 26
3

You could use references.

float premserv (int& daymin, int& nightmin)
{
     float daytotal = 0;     //Var for day total
     float nighttotal = 0;   //Var for night total
     float premserv = 0;     //Var for premium service cost

     cout << "\n" << "Please enter the number of minutes used durring the day (6AM - 6PM): " << "\n";
     cin >> daymin;
     cout << "\n" << "Please enter the number of minutes used durring the night (6PM - 6AM): " << "\n";
     cin >> nightmin;

     daytotal = (daymin - 75) * 0.1;
     nighttotal = (nightmin - 100) * 0.05;
     premserv = 25 + daytotal + nighttotal;

     return premserv;
}

This will directly modify the values of daymin and nightmin and you could still keep premserv as a returned value (since it fits with the function name, I guess).

You'd use it like so:

int daymin = 0;
int nightmin = 0;
float premiumservice = premserv(daymin, nightmin);
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Jesse Emond
  • 7,180
  • 7
  • 32
  • 37
1

in c, create a structure to hold above said 3 values. in c++ create a class and return an object of the class.

Ravi Bhatt
  • 3,147
  • 19
  • 21
1
float premserv(int& daymin, int& nightmin) { 
    float daytotal = 0;     //Var for day total    
    float nighttotal = 0;   //Var for night total    
    float premserv = 0;     //Var for premium service cost    
    // etc...

'premserv' is already being returned.

James
  • 9,064
  • 3
  • 31
  • 49
1

Another option besides using out parameters or defining a specific struct return type is to use tuples from C++11 or TR1:

/* Return Value: tuple<daymin,nightmin,premserv>   */
/***************************************************/

std::tuple<int,int,float> premserv ()
{
  // ...
  return std::make_tuple(daymin, nightmin, premiumservice);
}

Then you can use it like this:

int daymin, nightmin;
float premiumservice;
std::tie(daymin,nightmin,premiumservice) = premserv();

Or if you just care about one of the values:

int nightmin = std::get<1>(premserv());
bames53
  • 86,085
  • 15
  • 179
  • 244
0

You got multipel answers but alternatively you can return struct in the function parameter so you have one object to deal with and return value will tell you if the data is valid or not.

struct
{
   float premserv;
   int daymin;
   int nightmin;

} data;

bool premserv (data & my_data)
{
   // ...
}

if( premserv(my_data) == true )
{
  // user the data
}
zar
  • 11,361
  • 14
  • 96
  • 178