Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is part of the project already done: #include #include #include using namespace std; class Rain { private: const string filename = Rain.txt; ifstream inRain;

Here is part of the project already done:

#include #include #include

using namespace std;

class Rain {

private: const string filename = "Rain.txt"; ifstream inRain; //input file containing the daily rain values bool fileError; //Determines whether there was an error or not float sumRain=0; //holds the sum of the values from the input file float avgRain=0; //calculated rain average for the month int numOfDays; //Number of days in the month string monthName; //Name of the month

public: Rain() { cout

//open the input file void openFile() { cout

}

//test if the input file opened properly void testFile() { if (inRain.fail()) { cout

} }

//read the contents of the input file and add to the variable sumRain void readFile() { char inValue[100];

while (inRain >> inValue) { sumRain += atof(inValue);

cout

numOfDays++; }

}

//close the input file void closeFile() { inRain.close();

}

void calcAvg() {

}

void display() {

}

float getAvgRain() { return avgRain; }

float getSumRain() { return sumRain; } }; int main() { Rain cloud;

cloud.openFile(); cloud.testFile(); cloud.readFile(); cloud.closeFile();

return 0; }

image text in transcribedimage text in transcribedimage text in transcribed
Write a C++ program that will use object-oriented principles to read in a file of the daily rainfall for the month of June, calculate the average rainfall, and display the result. The program should read in the 30 values from the input file - JuneRain. txt - and calculate and display the average rainfall for the month to 3 decimal places. The input file should be located in the local directory. At a minimum, the program should contain: a class named Rain private member variables: ifstream inRain - input file containing the daily rain values float sumRain - holds the sum of the values from the input file float avgRain - calculated rain average for June public member functions: Rain () - constructor to initialize values void openFile () - open the input file void testFile () - test if the input file opened properly void readFile () - read the contents of the input file and add to the variable sumRain void closeFile () - close the input file void calcAvg () - calculate the average and populate the avgRain variable void display () - display the output value(s)\fSample file: Rain.txt . Make the program more useful (i.e. to be able to run for any month and calculate correctly but filename would just be Rain.txt). To do so consider what 1-2 additional attributes are needed. The following shows what the output should look like for a valid file. Program to average the rainfall for values in a file. Written by James Nichols The sum of the values is: 81.24 The average of the values is: 2.708 The following shows what the output should look like if the file isn't found. Program to average the rainfall for values in a file. Written by James Nichols Error opening the file 'Rain. txt'

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions