简体   繁体   中英

C++ How to return multiple arrays of different types from a function

I am trying to write a function that accepts 3 different arrays. These arrays are of type string, double, and double, respectively. The function will fill these arrays with data and then return them to main. However, I am unsure what to declare as the return type for the function, as all of the arrays do not hold the same data types. The function that will accept the arrays as arguments is listed below

void additems(string arry1[], double arry2[], double arry3[], int index)
{
/*************************  additems **************************
NAME: additems
PURPOSE: Prompt user for airport id, elevation, runway length.  Validate input and add to 3 seperate parallel arrays
CALLED BY: main
INPUT: airportID[], elevation[], runlength[], SIZE
OUTPUT: airporID[], elevation[], runlength[]
****************************************************************************/
    //This function will prompt the user for airport id, elevation, and runway     length and add them to 
    //separate parallel arrays
    for (int i=0; i<index; i++)
    {
        cout << "Enter the airport code for airport " << i+1 << ". ";
        cin >> arry1[i];
        cout << "Enter the maximum elevation airport " << i+1 << " flys at (in ft). ";
        cin >> arry2[i];
        while (arry2[i] <= 0)
        {
            cout << "\t\t-----ERROR-----";
            cout << "\n\t\tElevation must be greater than 0";
            cout << "\n\t\tPlease re enter the max elevation (ft). ";
            cin >> arry2[i];
        } 
        cout << "Enter the longest runway at the airport " << i+1 << " (in ft). ";
        cin >> arry3[i];
        while (arry3[i] <= 0)
        {
            cout << "\t\t-----ERROR-----";
            cout << "\n\t\tRunway length must be greater than 0";
            cout << "\n\t\tPlease re enter the longest runway length (ft). ";
            cin >> arry3[i];
        }
        cout << endl;
    }   

    return arry1, arry2, arry3;
   }

Thanks in advance for considering my question

You do not need to return the arrays, as they are modified by the function. When you pass arrays to a function like this, the array is passed by reference. Normally a data type is passed by value ( ie copied), but arrays are treated a little like pointers.

So just return void , or if you like you could return some kind of value to indicate success (if that's appropriate). You may want to return an integer to say how many records were entered (if the user has the option to enter less than index records).

You can just say

return;

or leave it out altogether. You're modifying the passed-in arrays in-place, so there's no need to return anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM