Question: Reading and displaying text file rainfall.txt. Do not assume the number of months. Instead, continue to read month names and rainfall amounts until the file
Reading and displaying text file "rainfall.txt.
"Do not assume the number of months. Instead, continue to read month names and rainfall amounts until the file is empty. You can assume that if a name is provided it will be followed by a rainfall amount. That means that you can test if the file is empty (using a while loop) by reading the name of the next month (see program 5-20) As you loop over the months in the file, keep a running total of rainfall amounts and a count of months processed. The count of months processed will be needed to calculate the average. Your output should be (using the names and amounts read from the file, of course):"
output should look like this:
Month Amount -------------------------------------------------------------------------- August 2.45 September 1.22 October 2.13 November 1.01 Average Rainfall: 1.70
Now here's my code so far, and it's not working:
#include "stdafx.h" #include
int main() { ifstream myfile("rainfall.txt"); //string textf[50];
myfile.open("rainfall.txt");
//error check if (myfile.fail()) { cout << "file read failure, check source" << endl; }
string textf[50]; myfile >> textf;
return 0;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
