My program works, but I'm really new at programming and am just curious if anything I wrote could be written better or more efficiently. It's suppose to display rainfall totals for the previous 12 months and say if they are + or - the average of those months. My teacher this semester gives no feedback or criticism on projects so I feel this is my best chance to learn something. It basically has two parts, calculate the previous 12 months and gets the average and then displays the months in a table format. I was wondering if I should bother trying to make each part it's own separate function or if it's fine written all in the main function.
int main() {
bool endProgram = 0, validInput = 0;
int currentMonth = 0, printedMonth = 0;
float avgRainfall = 0.0, tempTotal = 0.0;
string monthArray[MONTHS] =
{ "January","February", "March","April","May","June","July","August","September","October","November","December" };
float rainPerMonth[2][12] = {
{ 2.88, 3.89, 3.34, 3.35, 2.81, 1.18, 0.87, 1.84, 1.35, 6.14, 3.02, 3.22 }, //2016
{ 2.49, 0.28, 3.01, 3.09, 2.46, 4.01, 2.27, 2.24, 3.75, 2.06, 2.15, 4.41 } //2015
};
while (!endProgram) {
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
bool validInput = 0;
//User Inputs
while (!validInput) {
cout << "Please enter the number of the current month: ";
cin >> currentMonth;
if (currentMonth >= 1 && currentMonth <= 12) {
validInput = true;
//Calc the avg rainfall for the range we've selected
for (int i = 0; i < MONTHS; i++) {
if (currentMonth == 0) { //if we've hit jan
int ml = MONTHS - i;
currentMonth = MONTHS; //calculate how many months we have left
for (int j = 0; ml > 0; j++) {
tempTotal += rainPerMonth[0][currentMonth - 1];
currentMonth -= 1;
ml -= 1;
i++;
}break;
}
tempTotal += rainPerMonth[1][currentMonth - 1];
currentMonth -= 1;
}
currentMonth == 0 ? currentMonth = 12 : currentMonth = currentMonth;
avgRainfall = tempTotal / MONTHS;
cout << "Average rainfall for the last 12 months was: " << avgRainfall << std::endl;
cout << endl;
//Print the column headers
cout << "|" << setw(10) << ("Month ") <<
"|" << setw(10) << ("2016 ") <<
"|" << setw(10) << ("2015 ") <<
"|" << setw(22) << (" Difference ") <<
"|" << endl;
cout << setfill('=') << setw(56) << " " << setfill(' ') << endl;
//Print the monthly rainfall for 2016
for (int i = 0; i < MONTHS; i++) {
float diff = 0.0;
string diff2 = "";
//calc + or - average
diff = rainPerMonth[0][currentMonth - 1] - avgRainfall;
diff2 = (diff < 0) ? "Below average" : "Above average";
//Prints display
cout << "|" << setw(10) << left << monthArray[currentMonth - 1] <<
"| " << setw(7) << left << rainPerMonth[0][currentMonth - 1] <<
"| " << setw(7) << left << rainPerMonth[1][currentMonth - 1] <<
"| " << setw(0) << abs(diff) << " " << diff2 <<
" |" << std::endl;
currentMonth -= 1;
//If we need to go to 2015
if (currentMonth == 0) {
int ml = MONTHS - (i + 1); //months left to include
currentMonth = MONTHS;
//Prints the monthly rainfall for 2015
for (int j = 0; ml > 0; j++) {
//calc for + or - average
diff = rainPerMonth[1][currentMonth - 1] - avgRainfall;
diff2 = (diff < 0) ? "Below average" : "Above average";
//Prints display
cout << "|" << setw(10) << left << monthArray[currentMonth - 1] <<
"| " << setw(9) << left << "NO DATA" <<
"| " << setw(7) << left << rainPerMonth[1][currentMonth - 1] <<
"| " << setw(0) << abs(diff) << " " << diff2 <<
" |" << std::endl;
currentMonth -= 1;
ml -= 1;
i++;
}
}
}
} else {
cout << "Please enter a number between 1 and 12" << endl;
}
endProgram = RestartProgram();
}
}
return 0;