60 734
Assignments Done
Successfully Done
In April 2018

Answer to Question #55086 in C++ for kklljjll

Question #55086
Using Files—Total and Average Rainfall
Write a program that reads in from a file a starting month name, an ending month name,
and then the monthly rainfall for each month during that period. As it does this, it should
sum the rainfall amounts and then report the total rainfall and average rainfall for the
period. For example, the output might look like this:
During the months of March–June the total rainfall was 7.32 inches and the average
monthly rainfall was 1.83 inches.
Data for the program can be found in the Rainfall.txt file.
Hint: After reading in the month names, you will need to read in rain amounts until
the EOF is reached, and count how many pieces of rain data you read in.

Please use notepad to create rainfall.txt with following data.

2.35 1.15 2.03 1.41
Expert's answer

#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <cstdlib>

using namespace std;

int generate_stars(int n);
void read_and_count_rainfalls(string file_name);

int main()
string file_name = "rainfall.txt";
return 0;

void read_and_count_rainfalls(string file_name)
string first_month, last_month;
ifstream file;
getline(file, first_month);
getline(file, last_month);
vector <double> month_rainfall;
double temp;
double total = 0, average = 0;
string s_temp;
while (!file.eof())
file >> s_temp;
temp = atof(s_temp.c_str());
total += temp;
average = total / month_rainfall.size();
cout << "During the months of " << first_month << "-" << last_month << " the total rainfall was "
<< total << " inches and the average monthly rainfall was " << average << " inches" << endl;

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

Privacy policy Terms and Conditions