简体   繁体   English

对于循环灾难,它会进行更多的迭代

[英]For loop disaster, iterates more than it should

Can someone help me find out what this would iterate 3 times when i tell it to only do one time int the forloop? 当我告诉它仅在inforloop中执行一次时,有人可以帮我找出这将重复3次吗? I choose my mtype 1 and when i go to display that information(which this function is designed to do) it loops the 3 times instead of one. 我选择我的mtype 1,当我显示该信息(此功能旨在执行此信息)时,它将循环3次而不是1次。

EDIT: the reason i know its iterating more than it should is because the cout statement at the bottom of each if statement doesnt actually go there, im just using it to find where the problem is. 编辑:之所以我知道它比它应该迭代更多,是因为每个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;

} }

Look at all that repetition. 看所有的重复。 Try this: 尝试这个:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM