'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.
June
September
2.35 1.15 2.03 1.41
--
I am using Xcode on Mac. I don't know if that has anything to do with not being able to read the rainfall.txt
Every time I run the program, it spits out "0" is the total rainfall and an "n" appears in the front of " is the average amount of rainfall.\n"
Can someone please give me an idea of what I'm doing wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main ()
{
string begMonth,
endMonth;
ifstream inputFile;
double months = 0,
monthlyRain,
averageRainfall,
totalRainfall = 0.0;
inputFile.open("rainfall.txt");
inputFile >> begMonth;
inputFile >> endMonth;
//Calculations
totalRainfall += monthlyRain;
averageRainfall = totalRainfall / months;
cout << totalRainfall << " is the total amount of rainfall.\n";
cout << averageRainfall << " is the average amount of rainfall.\n";
return 0;
}
|