当我将循环设置为迭代 10 次时,它可以工作,但是无论我设置多少次迭代,它总是在第 12 次出现此错误。这是下面的代码。
有谁知道为什么会这样?或者如果我错过的代码中有逻辑错误。谢谢
string* Analyser::topFiveBuyers()
{
//set size and add buyer names for comparison.
string *calcString = new string[sSize];
int calcTotal[sSize] = {INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN};
//checks transactions
for (int i = 0; i<nTransactions; i++)
{
//compares with arrays
for(int j =0; j<sSize; j++)
{
if(calcTotal[j] < calcTotal[j+1])
{
int tVar = calcTotal[j+1];
string tStr = calcString[j+1];
int tVarTwo = calcTotal[j];
string tStrTwo = calcString[j];
calcTotal[j] = tVar;
calcString[j] = tStr;
calcTotal[j+1] = tVarTwo;
calcString[j+1] = tStrTwo;
}
if(tArray[i].buyerName == calcString[j])
{
calcTotal[j] += tArray[i].numShares;
break;
}
else
{
//checks if shares is great then current total then replaces
if(tArray[i].numShares > calcTotal[j])
{
int tVar = calcTotal[j];
calcTotal[j+1] = tVar;
string tStr = calcString[j];
calcString[j+1] = tStr;
calcTotal[j] = tArray[i].numShares;
calcString[j] = tArray[i].buyerName;
break;
}
}
}
}
return calcString;