Question: In c + + why is my output different from the expected output. Please help. Here's my code: #include #include using namespace std; int main

In c++ why is my output different from the expected output. Please help.
Here's my code:
#include
#include
using namespace std;
int main(){
double rainfall[12]={0.00}; // Create an array to store rainfall data for each month
string months[]={"1","2","3","4","5","6","7","8","9","10","11","12"};
double getTotal =0.00;
double getLargest =0.00;
double getSmallest =0.00;
string largestMonth, smallestMonth;
// Loop through each month to collect rainfall data
for (int i =0; i <12; i++){
bool validInput = false;
while (!validInput){
cout << "Enter the rainfall (in inches) for month #"<< months[i]<<": ";
cin >> rainfall[i];
// Input verification: Ensure input is not negative
if (rainfall[i]<0){
cout << "Rainfall must be 0 or more." << endl;
cout << "Please re-enter: ";
} else {
validInput = true; // If the input is valid, exit the input loop
getTotal += rainfall[i]; // Add total monthly rainfail
// Check for largest amount of rainfall
if (rainfall[i]> getLargest || i ==0){
getLargest = rainfall[i];
largestMonth = months[i];
}
// Check for smallest amount of rainfall
if (rainfall[i]< getSmallest || i ==0){
getSmallest = rainfall[i];
smallestMonth = months[i];
}
}
}
}
cout << endl;
double getAverage = getTotal /12.0; // Monthly average
// Display the requested statistics
cout << "The total rainfall for the year is "<< fixed << setprecision(2)<< getTotal <<" inches." << endl;
cout << "The average rainfall for the year is "<< fixed << setprecision(2)<< getAverage <<" inches." << endl;
cout << "The largest amount of rainfall was "<< fixed << setprecision(2)<< getLargest <<" inches in month "<< largestMonth <<"."<< endl;
cout << "The smallest amount of rainfall was "<< fixed << setprecision(2)<< getSmallest <<" inches in month "<< smallestMonth <<"."<< endl;
cout << endl;
cout << "Here are the rainfall amounts, sorted in ascending order:" << endl;
cout <<"----------------------------------------"<< endl;
for (int i =0; i <12; i++){
for (int j = i +1; j <12; j++){
if (rainfall[j]< rainfall[i]){
swap(rainfall[j], rainfall[i]);
swap(months[j], months[i]);
}
}
cout << rainfall[i]<< endl;
}
return 0;
}
Im getting an output error and compilation error, please help.
My output:
Enter the rainfall (in inches) for month #1: Rainfall must be 0 or more.
Please re-enter: Enter the rainfall (in inches) for month #1: Enter the rainfall (in inches) for month #2: Enter the rainfall (in inches) for month #3: Enter the rainfall (in inches) for month #4: Enter the rainfall (in inches) for month #5: Enter the rainfall (in inches) for month #6: Enter the rainfall (in inches) for month #7: Enter the rainfall (in inches) for month #8: Enter the rainfall (in inches) for month #9: Enter the rainfall (in inches) for month #10: Enter the rainfall (in inches) for month #11: Enter the rainfall (in inches) for month #12:
The total rainfall for the year is 78.00 inches.
The average rainfall for the year is 6.50 inches.
The largest amount of rainfall was 12.00 inches in month 12.
The smallest amount of rainfall was 1.00 inches in month 1.
Here are the rainfall amounts, sorted in ascending order:
----------------------------------------
1.00
2.00
3.00
4.00
5.00
6.00
7.00
8.00
9.00
10.00
11.00
12.00
Expected output:
Enter the rainfall (in inches) for month #1: Rainfall must be 0 or more.
Please re-enter: Enter the rainfall (in inches) for month #2: Enter the rainfall (in inches) for month #3: Enter the rainfall (in inches) for month #4: Enter the rainfall (in inches) for month #5: Enter the rainfall (in inches) for month #6: Enter the rainfall (in inches) for month #7: Enter the rainfall (in inches) for month #8: Enter the rainfall (in inches) for month #9: Enter the rainfall (in inches) for month #10: Enter the rainfall (in inches) for month #11: Enter the rainfall (in inches) for month #12:
The total rainfall for the year is 78.00 inches.
The average rainfall for the year is 6.50 inches.
The largest amount of rainfall was 12.00 inches in month 12.
The smallest amount of rainfall was 1.00 inches in month 1.
Here are the rainfall amounts, sorted in ascending order:
----------------------------------------
1.00
2.00
3.00
4.00
5.00
6.00
7.00
8.00
9.00
10.00
11.00
12.00

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!