Can a function return multiple data types?

I am taking a C++ class and I am trying to create a function that returns char and double date types. I have the program running fine without the function. It reads data from a file (employee id, fname, lname, hours worked, hourly rate), calculates gross pay, taxes, netpay, ot pay, etc and prints it to the screen. I included the peice of code I want in the function below. I also included the working code.
Any help would be greatly appreciated!
Regards,
Joel

I need the function the perform the following:
======================================
ifstream fin ("employee.txt");
while (fin>>empid[counter]>>fname[counter]>>lastname[counter]>>hw[counter]>>hr[counter])
======================================
My program has a few other functions that are working fine but they olny return a single data type.
i.e.
======================================
int regHours (int totalhours){
int regularhours;
if(totalhours>40) regularhours=40;
else regularhours=totalhours;
return (regularhours);}
--------------------------------------
rh[i] = regHours (hw[i]);
======================================

My text file contains the following data which I am able to read fine and using the program without the function:

1111 fname1 lname1 30 10
2222 fname2 lname2 40 11
3333 fname3 lname3 50 12
4444 fname4 lname4 60 13
5555 fname5 lname5 70 10

I am reading this data from the file and putting it into an array. The first three columns are char data types, the last two are double.

I tried to do the following in a few different variations but I am stuck.I searched the forum, and the tutorials and found info on templates that might work but I am having trouble interpreting from how it is used in the examples to how I need to use it.

Here's what I tried:
function...
================================================================
void fromFile (char c1, char c2, char c3, double c4, double c5){
ifstream fin ("employee.txt");
while (fin>>c1>>c2>>c3>>c4>>c5)
return (char c1, char c2, char c3, double c4, double c5);}
----------------------------------------------------------------
calling the function...
----------------------------------------------------------------
empid[i], fname[i], lastname[i], hw[i], hr[i] = void fromFile (empid[i], fname[i], lastname[i], hw[i], hr[i]);
================================================================


I know I am probably way off.
Here's all the code:
================================================================
================================================================
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
//functions/////////////////////////////////////////////////////////////////////
//////regHours function/////////////////////////////////////////////////////////
int regHours (int totalhours){
int regularhours;
if(totalhours>40) regularhours=40;
else regularhours=totalhours;
return (regularhours);}
//////otHours function//////////////////////////////////////////////////////////
int otHours (int totalhours){
int overtimehours;
if (totalhours>40) overtimehours=totalhours-40;
else overtimehours=0;
return (overtimehours); }
//////otPay function//////////////////////////////////////////////////////
double otPay (int otHours, double rate){
double overtimePay;
overtimePay = otHours*rate*1.5;
return (overtimePay);}
////////////////////////////////////////////////////////////////////////

main(){ //--MAIN START---------------------------------------------------------
char empid[100][12];
char fname[100][14], lastname[100][15];
int hw[100],rh[100],oth[100];
double gp[100],np[100],hr[100],taxrate[100],taxamt[100],ot[100],otp[100],rp[100];
int counter = 0;
int i;

ifstream fin ("employee.txt");
while (fin>>empid[counter]>>fname[counter]>>lastname[counter]>>hw[counter]>>hr[counter])
counter=counter+1;


//=================================
for (i=0;i<counter;i++)
{
//variable name = function name (variable(s) passing in)
rh[i] = regHours (hw[i]);
oth[i] = otHours (hw[i]);
ot[i] = otPay (oth[i], hr[i]);
}
for (i=0;i<counter;i++){
rp[i] = rh[i]*hr[i];}

for (i=0;i<counter;i++){
gp[i] = rp[i]+ot[i];}

for (i=0;i<counter;i++){
if (gp[i]>500) taxrate[i]=.30;

else if (gp[i]>200)taxrate[i]=.20;

else taxrate[i]=.10;
}//FOR

for (i=0;i<counter;i++){
taxamt[i]=gp[i]*taxrate[i];}//end taxamount for loop
for (i=0;i<counter;i++){
np[i]=gp[i]-taxamt[i];}//end of netpay for loop

cout<<endl;
cout<<"\t\tROCHESTER NY PROPERTIES, LLC PAYROLL"<<endl;

cout<<endl;
cout<<endl;


cout<<setw(14)<<"EMPLOYEE ID"<<setw(16)<<"FIRST NAME"<<setw(17)
<<"LAST NAME"<<setw(4)<<"HRS"<<setw(5)<<"RATE"<<setw(6)
<<"GROSS"<<setw(6)<<"TAX"<<setw(9)<<"NET PAY"<<endl<<endl;

cout<<setw(14)<<"==========="<<setw(16)<<"========="<<setw(17)
<<"========="<<setw(4)<<"==="<<setw(5)<<"===="<<setw(6)
<<"====="<<setw(6)<<"==="<<setw(9)<<"======="<<endl<<endl;

for (i=0;i<counter;i++){


cout<<setw(14)<<empid[i]<<setw(16)<<fname[i]<<setw(17)<<lastname[i]<<setw(4)
<<hw[i]<<setw(5)<<hr[i]<<setw(6)<<gp[i]<<setw(6)<<taxamt[i]
<<setw(9)<<np[i]<<endl;

}//FOR
system("pause");

return 0;
}//--MAIN END-------------------------------------------------------------------






For starters, your function is void, so it shouldn't be returning anything.
Short answer: No. A function can return only one value.
Long answer:
You can pass multiple values back to the parent function in a few different ways.
1. Create a data container (struct/class) containing the values you want to return. Then just update your function to return the class type rather than void and change your return value to return the class. This way you will be able to return all the data, but still only return one item.
2. Use pointers. Keep your function returning void, and don't return any value. Just use "return;" Instead in your function arguments, include pointers to whatever you want to 'return'. Define a variable or pointer in the parent function and pass the address or value when you call the child function. This will allow the child function to change the actual variable defined in the parent function, but it will not have to return it because it modifies the original variable rather than a copy of it.
I prefer the second method, and use it quite frequently because it allows you to use the return value for something more informational, such as an int or bool defining the success/failure of the function and possible pass an error code back to the parent function.
Topic archived. No new replies allowed.