El Spiffy
El Spiffy

Reputation: 135

The for loop keeps looping

So I'm putting together this program that takes 4 values for each month of the year. 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. 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 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";                
}
}

Upvotes: 0

Views: 244

Answers (2)

Harsh
Harsh

Reputation: 399

You seem to have two for loops. The code would ask for 12 years in fact which I guess is not what you wanted. I'd have added this as a comment but I can't (my reputation is too low!).

Upvotes: 0

Hatted Rooster
Hatted Rooster

Reputation: 36463

Because your for loops are nested you're basically looping 12 * 12 = 144 times. The outer loop loops 12 times and per 1 outer loop you loop 12 times through every month. This is probably not intended.

Upvotes: 2

Related Questions