我正在尝试编写一个接受3个不同数组的函数。这些数组分别是string,double和double类型。该函数将使用数据填充这些数组,然后将它们返回到main。但是,我不确定要声明什么作为函数的返回类型,因为所有数组都不包含相同的数据类型。将接受数组作为参数的函数列在下面
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;
}
提前感谢您考虑我的问题
答案 0 :(得分:4)
您无需返回数组,因为它们已被函数修改。将数组传递给这样的函数时,数组通过引用传递。通常,数据类型按值传递(即复制),但数组的处理方式有点像指针。
所以只需返回void
,或者如果您愿意,可以返回某种值来表示成功(如果合适)。您可能希望返回一个整数来表示输入的记录数(如果用户可以选择输入少于index
个记录)。
答案 1 :(得分:1)
你可以说
return;
或完全不管它。您正在修改传入的数组,因此无需返回任何内容。