C++ 对于循环灾难,迭代次数超出了应有的次数
有人能帮我找出当我告诉它在forloop中只做一次时,它会迭代3次什么吗?我选择我的mtype 1,当我去显示这个函数设计用来做的信息时,它会循环3次而不是一次 编辑:我之所以知道它的迭代次数比应该的多,是因为每个if语句底部的cout语句实际上并没有出现,我只是用它来找出问题所在C++ 对于循环灾难,迭代次数超出了应有的次数,c++,C++,有人能帮我找出当我告诉它在forloop中只做一次时,它会迭代3次什么吗?我选择我的mtype 1,当我去显示这个函数设计用来做的信息时,它会循环3次而不是一次 编辑:我之所以知道它的迭代次数比应该的多,是因为每个if语句底部的cout语句实际上并没有出现,我只是用它来找出问题所在 for (int itemno = 0; itemno < numItems; itemno++) { if (mtype == *(type + itemno) || mtype == -1)
for (int itemno = 0; itemno < numItems; itemno++)
{
if (mtype == *(type + itemno) || mtype == -1)
{
if (mtype == 2)
{
*(price + itemno) = calculatePrice(*(cost + itemno), *(hours + itemno));
totalCost = totalCost + *(cost + itemno);
totalHour = totalHour + *(hours + itemno);
totalPrice = totalPrice + *(price + itemno);
cout << setw(13) << left << canType << setw(13) << right << *(cost + itemno) << setw(16) << setprecision(2) << fixed << hours[itemno] << setw(16) << price[itemno] << endl << endl;
}
}
else if (mtype == *(type + itemno) || mtype == -1)
{
if (mtype == 3)
{
*(price + itemno) = calculatePrice(*(cost + itemno), *(hours + itemno));
totalCost = totalCost + *(cost + itemno);
totalHour = totalHour + *(hours + itemno);
totalPrice = totalPrice + *(price + itemno);
cout << setw(13) << right << *(cost + itemno) << setw(16) << setprecision(2) << fixed << hours[itemno] << setw(16) << price[itemno] << endl << endl;
}
}
else if (mtype == *(type + itemno) || mtype == -1)
{
if (mtype == 4)
{
*(price + itemno) = calculatePrice(*(cost + itemno), *(hours + itemno));
totalCost = totalCost + *(cost + itemno);
totalHour = totalHour + *(hours + itemno);
totalPrice = totalPrice + *(price + itemno);
cout << setw(13) << right << *(cost + itemno) << setw(16) << setprecision(2) << fixed << hours[itemno] << setw(16) << price[itemno] << endl << endl;
}
}
else if (mtype == *(type + itemno) || mtype == -1)
{
if (mtype == 5)
{
*(price + itemno) = calculatePrice(*(cost + itemno), *(hours + itemno));
totalCost = totalCost + *(cost + itemno);
totalHour = totalHour + *(hours + itemno);
totalPrice = totalPrice + *(price + itemno);
cout << setw(13) << right << *(cost + itemno) << setw(16) << setprecision(2) << fixed << hours[itemno] << setw(16) << price[itemno] << endl << endl;
}
}
cout << setw(13) << right << *(cost + itemno) << setw(16) << setprecision(2) << fixed << hours[itemno] << setw(16) << price[itemno] << endl << endl;
}
cout << endl << setw(9) << left << "TOTALS: " << setw(5) << right << "$" << setw(12) << totalCost<< setw(17) << totalHour << setw(17) << "$" << setw(5) << totalPrice << endl;
cout << endl << horizontalLine << endl << endl;
}看看那些重复。试试这个:
if (mtype >= 2 && mtype <= 5) {
for (int itemno = 0; itemno < numItems; itemno++) {
if (mtype == type[itemno]) {
price[itemno] = calculatePrice(cost[itemno], hours[itemno]);
totalCost += cost[itemno];
totalHour += hours[itemno];
totalPrice += price[itemno];
if (mtype == 2) cout << setw(13) << left << canType;
cout << setw(13) << right << cost[itemno]
<< setw(16) << setprecision(2) << fixed << hours[itemno]
<< setw(16) << price[itemno] << "\n\n";
}
}
}
cout << "\n" << setw(9) << left << "TOTALS: "
<< setw(5) << right << "$" << setw(12) << totalCost
<< setw(17) << totalHour
<< setw(17) << "$" << setw(5) << totalPrice << "\n\n";
cout << horizontalLine << "\n\n";
cout << flush;
numItems的值是什么?要调试它,我建议您在debugger中打印或查看for循环中循环控制变量itemno和numItems在开始和结束时的值。numItems是一个清单,当用户向其中添加单个糖果时会更新。所以如果你通过这个程序并添加3块NUMTOBE,3的糖果类型被存储在MyType中,当NUMATION更新时,@ SJ0H我不知道如何做,我对整个C++的东西都很新。演示了一个好的编码实践,但它实际上并没有回答问题。整个for循环?@Beta没有足够的信息来回答这个问题,他的评论也不可能作为评论。当信息可用时,我相信他会适当地更新他的答案。在这种情况下我倾向于宽容。