Question: *C++* PLEASE REFERENCE CODE I HAVE WRITTEN (IMAGE ATTACHED) WHEN SUGGESTING SOLUTIONS! I want to be able to learn why my code is not working

*C++* PLEASE REFERENCE CODE I HAVE WRITTEN (IMAGE ATTACHED) WHEN SUGGESTING SOLUTIONS! I want to be able to learn why my code is not working the way I expect it to.

Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month.

After all iterations, the program should display the number of months, the total inches of rainfall, and the average rainfall per month for the entire period.

Add additional loops to validate the input:

  • Do not accept a number less than 1 for the number of years. Continue to prompt the user for a number until a valid number is entered.
  • Do not accept negative numbers for the monthly rainfall. Continue to prompt the user for a number until a valid number is entered.

You must test your program using the below data. Your output must look just like that in the test run below. Dont forget to copy and paste your test run to the bottom of your source code. Name your program YourLastNameLab7.cpp.

Test Run

This program will calculate average rainfall over a

period of years. How many years do you wish to average? 0

Years must be at least one. Please re-enter: -2

Years must be at least one. Please re-enter: 2

Year 1

Number of inches of rain for month 1? 1.555

Number of inches of rain for month 2? 3.5

Number of inches of rain for month 3? 2

Number of inches of rain for month 4? 4

Number of inches of rain for month 5? 2

Number of inches of rain for month 6? 4

Number of inches of rain for month 7? 2

Number of inches of rain for month 8? 4

Number of inches of rain for month 9? 2

Number of inches of rain for month 10? 4

Number of inches of rain for month 11? 2

Number of inches of rain for month 12? 4

The rainfall for year 1 is 35.055 and the average rainfall for the year is 2.921

Year 2

Number of inches of rain for month 1? -2

Rainfall must be zero or greater.

Number of inches of rain for month 1? 3

Number of inches of rain for month 2? 4

Number of inches of rain for month 3? 3

Number of inches of rain for month 4? 4

Number of inches of rain for month 5? 3

Number of inches of rain for month 6? 4

Number of inches of rain for month 7? 3

Number of inches of rain for month 8? 4

Number of inches of rain for month 9? 3

Number of inches of rain for month 10? 4

Number of inches of rain for month 11? 2.665

Number of inches of rain for month 12? 3.3

The rainfall for year 2 is 40.965 and the average rainfall for the year is 3.414

Over a period of 24 months, 76.020 inches of rain fell.

Average monthly rainfall for the period is 3.168 inches.

This is what I have so far....

*C++* PLEASE REFERENCE CODE I HAVE WRITTEN (IMAGE ATTACHED) WHEN SUGGESTING SOLUTIONS!Issues I'm having:

1) It doesn't end and calculate after the 2nd year values are input.

2) My professor wants calculations after each year, in addition to total calculations at the end of program. I can't figure out how to implement that.

7 E#include #include using namespace std; int main() { //Variable declaration. int years = @; const int MONTHS = 12; //Always are 12 months in the year. Create a constant. float average = e.ef; float rainfall = e.ef; float total_rainfall = 0.of; cout > years; E 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 while (years > years; //Try validating again with new value. } for (int i = 1; 1 > rainfall; while (rainfall > rainfall; } total_rainfall += rainfall; //Calculate sum of rainfall values. Continue to add each month. //once all months have been accounted for, show output for calculations. cout

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!