Average Population Calculation Program

I need to write a program that sums and calculates the average for the data that is contained in the file.

Write a program that asks the user for the input file's name. Open the file and read each line. Sum and average the appropriate values. Once all of the input lines are read, write out the average county population for each state, both to the console and to an output file. The average value should also be formatted to have 1 decimal place using the fixed decimal format. So you should perform floating point arithmetic operations. Your program should have the following functions:

A function that opens the input and output files and sets the output of the floating-point number to one decimal place in a fixed decimal format.
A function that initializes the variables needed in the main program. (hint: you will need to use pass by reference in this function.)
A function that adds a county population to the current sum for a state.
A function that calculates the average county population for a state. (hint: this function should return a floating point value.)
A function that outputs the relevant results to the console and into the output file.

The data in the file is also separated by tabs. The problem is I can't seem to extract that data. I also don't know how to ask for a file and input it, instead of just declaring it as I did.

This is what I did so far. Any help would be appreciated thanks.


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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;

void Fileopen(ifstream& InputFile, ofstream& OutputFile);
void initializeVariables(ifstream& InputFile, ofstream& OutputFile);
void SumPopulation(float&, float&, ifstream&, ofstream&, int&, int&);
void averageCounty(int&, int&, float&, float&, ifstream&, ofstream&);
void output(int&, int&, float&, float&, ifstream&, ofstream&, float&, float&);

int main() {

	ifstream InputFile;
	ofstream OutputFile;
	Fileopen(InputFile, OutputFile);

	InputFile.close();
	OutputFile.close();

	return 0;
}

void Fileopen(ifstream& InputFile, ofstream& OutputFile)
{
	InputFile.open("data.txt");
	OutputFile.open("avgdata.txt");
	initializeVariables(InputFile, OutputFile);

	OutputFile << showpoint << fixed << setprecision(2);

}


void initializeVariables(ifstream& InputFile, ofstream& OutputFile) {
	string state;
	float population;
	string county;
	string line;
	string when = "FL";
	string what = "MS";
	int MS = 0;
	int FL = 0;
	float PopulationSumMS = 0;
	float PopulationSumFL = 0;

	
		while (!InputFile.eof()) {
			InputFile >> state >> county >> population;
			if (state == what)  {
				MS++;
				PopulationSumMS = PopulationSumMS + population;
				InputFile.ignore('\n');
			}
			if (state == when) {
				FL++;
				PopulationSumFL = PopulationSumFL + population;
				InputFile.ignore('\n');

			}
		}
		SumPopulation(PopulationSumMS, PopulationSumFL, InputFile, OutputFile, MS, FL);
		averageCounty(MS, FL, PopulationSumMS, PopulationSumFL, InputFile, OutputFile);
	}

	void SumPopulation(float& PopulationSumMS, float& PopulationSumFL, ifstream& InputFile, ofstream& OutputFile, int& MS, int& FL) {
	}

	void averageCounty(int& MS, int& FL, float& PopulationSumMS, float& PopulationSumFL, ifstream& InputFile, ofstream& OutputFile) {
		float AverageMS = PopulationSumMS / MS;
		float AverageFL = PopulationSumFL / FL;
		output(MS, FL, PopulationSumMS, PopulationSumFL, InputFile, OutputFile, AverageMS, AverageFL);
	}

	void output(int& MS, int&FL, float& PopulationSumMS, float& PopulationSumFL, ifstream& InputFile, ofstream& OutputFile, float& AverageMS, float& AverageFL) {
		OutputFile << "MS: " << AverageMS << endl;
		OutputFile << "FL: " << AverageFL << endl;
		InputFile.close();
		OutputFile.close();
	}

This is the data separated by tabs

FL Autauga 54571
FL Baldwin 182265
MS Barbour 27457
FL Bibb 22915
FL Bloun 57322
FL Bullock 10914
FL Butler 20947
FL Calhoun 118572
MS Chambers 34215
MS Cherokee 25989
FL Chilton 43643
MS Choctaw 13859
MS Clarke 25833
FL Clay 13932
FL Cleburne 14972
FL Coffee 49948
MS Colbert 54428
FL Conecuh 13228
FL Coosa 11539
MS Covington 37765
MS Crenshaw 13906
FL Cullman 80406
FL Dale 50251
FL Dallas 43820
FL DeKalb 71109
MS Elmore 79303
MS Escambia 38319
FL Etowah 104430
FL Fayette 17241
MS Franklin 31704
FL Geneva 26790
MS Greene 9045
FL Hale 15760
FL Henry 17302
FL Houston 101547
MS Jackson 53227
MS Jefferson 658466
FL Lamar 14564
MS Lauderdale 92709
MS Lawrence 34339
MS Lee 140247
MS Limestone 82782
FL Lowndes 11299
FL Macon 21452
MS Madison 334811
FL Marengo 21027
FL Marion 30776
MS Marshall 93019
FL Mobile 412992
MS Monroe 23068
FL Montgomery 229363
FL Morgan 119490
MS Perry 10591
MS Pickens 19746
FL Pike 32899
FL Randolph 22913
FL Russell 52947
FL St. Clair 83593
FL Shelby 195085
MS Sumter 13763
FL Talladega 82291
MS Tallapoosa 41616
MS Tuscaloosa 194656
FL Walker 67023
MS Washington 17581
MS Wilcox 11670
FL Winston 24484
Topic archived. No new replies allowed.