我似乎无法得到正确的平均值

时间:2016-05-30 03:04:19

标签: c++

我必须编写一个程序,在其中我总结和平均佛罗里达州和密西西比州的所有数据。我认为我做的一切都是正确的,除了,我似乎得到了错误的平均值。

输出应该是 MS:79075.5 FL:65785.2

我到了 MS:84123 FL:62151.4

这是我到目前为止的代码:

#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();
}

该文件具有由标签分隔的以下数据

FL Autauga 54571

FL Baldwin 182265

MS Barbour 27457

FL Bibb 22915

FL Bloun 57322

FL布洛克10914

FL巴特勒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达拉斯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华盛顿州17581

MS Wilcox 11670

FL Winston 24484

1 个答案:

答案 0 :(得分:2)

正如@Galik在评论中指出的那样,代码的一个问题是std::string的格式化输入读取单个单词但输入包含至少一行,其中中间条目包含两个单词({{1} })。完成家庭作业的原始要求包括提到字段是分隔符的规范。在这种情况下,最简单的方法是使用适当的St. Clair调用来读取县,例如:

std::getline()