我无法返回动态分配的两个数组。我有一个读取数组大小的外部文件。 (在我的情况下为20),当我使用动态分配的数组时,这就是使数组大小。
此外,一旦我返回它们,我当前的语法是否正确或是否有我应该改变的东西。
这是我的代码
int main (void)
{
int size;
int notFound = 0;
int accountNumber = 0;
int results;
int * accountPtr = nullptr;
double * balancePtr = nullptr;
size = readFile(notFound, accountPtr, balancePtr);
if (notFound == 0)
{
selectionSort(accountPtr, balancePtr, size);
while (accountNumber != -99)
{
cout << "Please Enter an Account Number (Type -99 to quit): ";
cin >> accountNumber;
if (accountNumber == -99)
{
cout << "Goodbye!" << endl;
}
else
{
results = binarySearch(accountPtr,accountNumber,size);
if ( results == -1)
{
cout << "That Account Number Does Not Exist." << endl;
}
else
{
cout << "\nAccount Number" << "\t\t" << "Account Balance" << endl;
cout << "-----------------------------------------------" << endl;
cout << fixed << setprecision(2) << accountPtr[results] << "\t\t\t" << balancePtr[results] << endl << endl;
}
}
}
}
return 0;
}
int readFile (int ¬Found, int *accountPtr, double *balancePtr)
{
int size;
ifstream inputFile;
inputFile.open("account.txt");
if (inputFile.fail())
{
cout << "The File Account.TXT was not found." << endl;
notFound = 1;
}
else
{
inputFile >> size;
unique_ptr<int[]> accountPtr(new int[size]);
unique_ptr<double[]> balancePtr(new double[size]);
for(int i = 0; i < size; i++)
{
inputFile >> accountPtr[i] >> balancePtr[i];
}
}
return size;
}
答案 0 :(得分:1)
你按价值传递指针。调用代码中的指针变量不会被修改。除了传递值之外,在函数中你还要声明与正式参数同名的局部变量。
相反,您应该通过引用std::vector
返回或传递。