对于循环灾难,迭代超过它应该

时间:2014-04-09 02:10:02

标签: c++

当我告诉它只在forloop中做一次时,有人可以帮我找出这会迭代3次吗?我选择我的mtype 1,当我去显示那些信息时(这个功能是设计用的)它循环3次而不是1次。

编辑:我知道它的迭代次数超过它应该的原因是因为每个if语句底部的cout语句实际上并没有去那里,我只是用它来找到问题所在。

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;

}

1 个答案:

答案 0 :(得分:0)

看看所有重复。试试这个:

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;
相关问题