我的循环似乎被打破了

时间:2015-02-05 16:32:34

标签: c++ loops

我的循环没有打印第6个条目。

它应该在该行上显示0 4 0,但它只是跳过它。

#include "stdafx.h"
#include <iostream>
using namespace std;

const int NUMPLANTTYPES = 7;

int main()
{
    double PlantNumbers[NUMPLANTTYPES] = {25, 56, 44, 120, 16, 0, 15};
    int PotSize[NUMPLANTTYPES] = {3, 4, 1, 2, 3, 4, 1};
    double TotalCosts[NUMPLANTTYPES] = { 10, 10, 3.5, 6.99, 10, 10, 3.5 };

    cout << "PlantSold\t PotSize\tTotalCosts " << endl;
    for (int MAXSALES = 0; MAXSALES < 7; MAXSALES++)
    {
        if (PlantNumbers[MAXSALES] > 8) 
        {
            float TotalCost = (PlantNumbers[MAXSALES] * TotalCosts[MAXSALES]);

            cout << PlantNumbers[MAXSALES] << "\t\t " << PotSize[MAXSALES] << "\t\t  " << TotalCost << endl;
        }
    }

    int a;
    cin >> a;

    return 0;
}

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

  

我的循环不打印第6个条目,有什么帮助吗?

是的,因为PlantNumbers[5]等于0,因为:

    if (PlantNumbers[MAXSALES] > 8)