Average Calculation

I have to write a program in which I sum and average all the data for the population of Florida, and Mississippi. I think I did everything right, except, I seem to be getting the wrong averages.

The output is supposed to be MS: 79075.5 FL: 65785.2

I am getting MS: 84123 FL: 62151.4

This is the code I have so far:
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
83
84
85
86
87
88
#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)
{
    cout << "Enter file: "; 
    string fileName;
    cin >> fileName;
    InputFile.open(fileName.c_str());
    if (InputFile.fail())
    {
        cout << "ERROR NO FILE FOUND" << endl;
    }

    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 >> state >> county >> population) {
        if (state == what)  {
            MS++;
            PopulationSumMS = PopulationSumMS + population;
        }
        else if (state == when) {
            FL++;
            PopulationSumFL = PopulationSumFL + population;
        }
    }
    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;
    cout << "FL: " << AverageFL << endl;
    cout << "MS: " << AverageMS << endl;
    InputFile.close();
    OutputFile.close();
}

The file has the following 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
Why is this empty?

1
2
void SumPopulation(float& PopulationSumMS, float& PopulationSumFL, ifstream& InputFile, ofstream& OutputFile, int& MS, int& FL) {
}
Yeah I know I fixed that, and I dont need help with my program anymore. Thanks
It stops reading input when it reaches the county with two words (St. Clair). How did you go about fixing that?
I used getline, and it works perfectly, I dont need help on this anymore, thanks.
Topic archived. No new replies allowed.