Reading and displaying text file \"rainfall.txt. \"Do not assume the number of m
ID: 3723290 • Letter: R
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 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<fstream>
#include<iostream>
#include<string>
using namespace std;
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;
Explanation / Answer
/*
After opening file connection we need to run a while till the end of file inside that loop we can read data from file
cin => function reads data without space means it reads till the space so I used one counter to separate month and amount
means if count is even then it is month name else it is amount.
so when one record month and amount complete read then record count incremented by one.
Sample Output:
Month Amount
----------------------------------
August 2.45
September 1.22
October 2.13
November 1.01
Average Rainfall: 1.70
*/
//Your Program start from below line.
#include "stdafx.h"
#include<fstream>
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
int main()
{
ifstream myfile;
//string textf[50];
myfile.open("rainfall.txt");
//error check
if (myfile.fail())
{
cout << "file read failure, check source" << endl;
exit(0);
}
char month[50];
float sum = 0;
float temp;
int count = 0;
int recordCount = 0;
cout << "Month Amount ";
cout << "---------------------------------- ";
//Reading till the last entry of file
while (myfile.eof() == 0)
{
//Count to separate month and amount, If it is amount increase record count and display record
if (count % 2 == 1)
{
//Reading float rainfall amount in float variable
myfile >> temp;
//adding
sum = sum + temp;
//Incrementing record count after each record read. month+amount
recordCount++;
//Displaying the file content
cout << month << " " << temp << endl;
}
else //Reading month in char array
{
myfile >> month;
}
count++;
}
//Calculating average
double average = sum / recordCount;
printf("Average Rainfall: %.2f ", average);
//Closing file connection
myfile.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.