I have written a code that takes in user data for average rainfall and actual ra
ID: 3824139 • Letter: I
Question
I have written a code that takes in user data for average rainfall and actual rainfall for each month in a year. I am trying to display a chart but do not know how to go about it. I know that you cannot use a string in a for loop but I cant figure out how else i could get the month to display in the chart. Any thoughts/suggestions?
#include <iostream>
#include <string>
using namespace std;
int main()
{
const string month[12] {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"};
float Average_Rainfall[12], Actual_Rainfall[12], above_Below_Average[12];
char ask;
do {
cout << "Please enter the average rainfall for each month. " << endl;
for (int i = 0; i < 12; i++) {
cout << month[i] << ": ";
cin >> Average_Rainfall[i];
while (Average_Rainfall[i] < 0){
cout << "You cannot have negative rain. Please enter a positive number.";
cin >> Average_Rainfall[i];
}
}
cout << " Please enter the Actual rainfall for each month. ";
for (int i = 0; i < 12; i++) {
cout << month[i] << ": ";
cin >> Actual_Rainfall[i];
while (Actual_Rainfall[i] < 0){
cout << "You cannot have negative rain. Please enter a positive number.";
cin >> Actual_Rainfall[i];
}
}
for (int i = 0; i < 12; i++) {
above_Below_Average[i] = Average_Rainfall[i] - Actual_Rainfall[i];
}
cout << "Month Average Actual Above/Below Average ";
cout << "----- ------- ------ ------------------- ";
for (string i = 0; i < 12; i++) {
for ( float j = 0; j < 12; i++) {
cout << month[i] << " " << Average_Rainfall[j] << " "
<< Actual_Rainfall[j] << " " << above_Below_Average[j] << endl;
}
}
cout << "Would you like to restart program? Y (yes) or N (No)";
cin >> ask;
while (ask != 'Y' && ask != 'y' && ask != 'N' && ask != 'n') {
cout << "You have entered an invalid response. Please enter a Y for yes or N for no. ";
cin >> ask;
}
} while (ask == 'Y' || ask == 'y');
return 0;
}
Explanation / Answer
Suggestions as requested by the Student :