简体   繁体   English

for循环不断循环

[英]The for loop keeps looping

So I'm putting together this program that takes 4 values for each month of the year. 因此,我将这个程序组合在一起,该程序每年的每个月都使用4个值。 The only issue I'm having is that after I put in the last input for December, the loop continues and starts over to January. 我唯一遇到的问题是,我输入了12月的最后一个输入后,循环继续,并开始到1月。 What am I forgetting? 我忘记了什么?

#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;

        }
}

Here's the void function, but I am sure this does not have anything to do with it. 这是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";                
}
}

Because your for loops are nested you're basically looping 12 * 12 = 144 times. 因为您的for循环是嵌套的,所以您基本上循环了12 * 12 = 144次。 The outer loop loops 12 times and per 1 outer loop you loop 12 times through every month. 外循环循环12次,每1个外循环每个月循环12次。 This is probably not intended. 这可能不是故意的。

You seem to have two for loops. 您似乎有两个 for循环。 The code would ask for 12 years in fact which I guess is not what you wanted. 该代码实际上要求了12年,我想这不是您想要的。 I'd have added this as a comment but I can't (my reputation is too low!). 我已经将此添加为评论,但是我不能(我的声誉太低了!)。

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

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