for循环保持循环

时间:2015-12-05 22:34:30

标签: c++ for-loop

所以我把这个程序放在一起,一年中的每个月需要4个值。我唯一的问题是,在我输入12月的最后一个输入后,循环继续并开始到1月。我忘记了什么?

#include <iostream>
#include <iomanip>

using namespace std;

enum Month {January,February,March,April,May,June,July,August,September,October,November,December };

void displayMonthName (Month );

struct Airport
{
int numLanded;
int numDeparted;
int mostLanded;
int leastLanded;    

};

int main ()
{
int count;
const int MAX = 12;
double total = 0.0;
double average;

Airport year[MAX];

Month months;


for (count = 0 ; count < MAX ; count++)
{
    for ( months = January; months <= December ; months= static_cast <Month>(months + 1))       
        {
            cout<< "Enter the number of planes landed in ";
            displayMonthName(months);
            cout<<"\t";
            cin>>year[count].numLanded;

            cout<< "Enter the number of planes that landed in ";
            displayMonthName(months);
            cout<<"\t";
            cin>>year[count].numDeparted;

            cout<< "Enter the greatest number of planes that landed on a single day in ";
            displayMonthName(months);
            cout<<"\t";
            cin>>year[count].mostLanded;

            cout<< "Enter the least number of planes that landed on a single day in ";
            displayMonthName(months);
            cout<<"\t";
            cin>>year[count].leastLanded;

            cout << endl;

        }
}

这是void函数,但我确信这与它没有任何关系。

void displayMonthName(Month m)
{
switch (m)
{
    case January    : cout<< "January";
                        break;
    case February   : cout<< "February";
                        break;
    case March      : cout<< "March";
                        break;
    case April      : cout<< "April";
                        break;
    case May        : cout<< "May";
                        break;
    case June       : cout<< "June";
                        break;
    case July       : cout<< "July";
                        break;
    case August     : cout<< "August";
                        break;  
    case September  : cout<< "September";
                        break;
    case October    : cout<< "October";
                        break;
    case November   : cout<< "November";
                        break;
    case December   : cout<< "December";                
}
}

2 个答案:

答案 0 :(得分:2)

由于您的 enemies.get(3).setHealth(Health.Died) // died enemy with number 3 循环已嵌套,因此基本上循环for次。外环循环12次,每1个外环循环,每个月循环12次。这可能不是故意的。

答案 1 :(得分:0)

循环似乎有两个 。该代码实际上要求12年,我猜这不是你想要的。我已将此添加为评论,但我不能(我的声誉太低了!)。