0

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";                
}
}
El Spiffy
  • 135
  • 5
  • Tried it. That only made it ignore December completely and continue with January. – El Spiffy Dec 05 '15 at 22:38
  • Possible duplicate of [What happens if you static\_cast invalid value to enum class?](http://stackoverflow.com/questions/18195312/what-happens-if-you-static-cast-invalid-value-to-enum-class) – LogicStuff Dec 05 '15 at 22:38
  • Yeah, but besides that, you're completely forgetting what you just wrote. `int x = 1;` - why is `x` equal to `1`? – LogicStuff Dec 05 '15 at 22:40
  • It could be much more simple working with `int` instead of this `enum` for months. E.g. `displayMonthName` function will be a const array of 12 strings selected by month index instead of this `switch-case`. – i486 Dec 05 '15 at 22:46

2 Answers2

2

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.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

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!).

Harsh
  • 389
  • 2
  • 18