Reading/Writing text file

Hi I have this question here,
Write a program that reads data from a text file that contains information about the rainfall amounts during a specific period and reports the total and average rainfall for the specified period. The text file will have the year, start month, and end month of the period in the first three lines of the file and then the rainfalls amounts in that period. 
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
31
32
33
34
35
36
37
38
39
40
41
42
  #include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main()
{
	ifstream myFile;
	ofstream outFile;
	
	myFile.open("AverageRainfall.txt");

	if (myFile && outFile) {
		int year;
		string month;
			
		double rainfall;
		double totalRain = 0.0;

		while (myFile >> year >>  month >> rainfall) {
			totalRain++;
			sum += total;
			cout << "During " << month << "-" << month
				<< year << "\t" << "The total rainfall was: "
				<< totalRain
				<< endl;
			}
		float average = sum / totalRain;
		outFile << "Rain average is: " << average << endl;
		myFile.close();
		
	}
	else {
		cout << "Error in opening the input/output text file" << endl;
	}
	
	system("pause");
    return 0;
}

and when I run this the notepad file doesn't get read and output, the file is with the source files aswell. Any help on where I could take this next?
Last edited on
closed account (48T7M4Gy)
This is a start to tidying up what you have.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <fstream>
#include <string>

using namespace std;

int main()
{
    int year = 0;
    string month;
    
    double rainfall = 0;
    double totalRain = 0;
    int number_of_records = 0;
    
    ifstream myFile;
    ofstream outFile;
    
    myFile.open("0000.txt");
    
    if (myFile && outFile)
    {
        //GET THE INTRO INFO
        if (myFile >> year >>  month )
        {
            cout << "During " << month << "-" << month << ' ' << year << endl;
        }
        else
        {
            cout << "Error 1\n" << endl;
            return 0;
        }
        
        //GET THE DETAIL INFO
        while(myFile >> rainfall)
        {
            number_of_records++;
            totalRain += rainfall;
        }
        
           cout << totalRain << endl;
        
        double average = totalRain / number_of_records;
        cout << "The total rainfall was: " << totalRain << endl;
        
        cout << "Rain average is: " << average << endl;
        myFile.close();
    }
    else
    {
        cout << "Error in opening the input/output text file" << endl;
    }
    
    return 0;
}


1979
April
1 2 3 4 5 6 7


During April-April 1979
28
The total rainfall was: 28
Rain average is: 4
Program ended with exit code: 0


You will need to have startMonth and endMonth variables ...
Topic archived. No new replies allowed.