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

标签 c++

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

输出应该是 女士:79075.5 佛罗里达州:65785.2

我得到 女士:84123 佛罗里达州: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 奥托加 54571

佛罗里达鲍德温 182265

巴伯女士 27457

FL 比伯 22915

FL 布隆 57322

佛罗里达布洛克 10914

佛罗里达巴特勒 20947

佛罗里达卡尔霍恩 118572

钱伯斯女士 34215

MS 切诺基 25989

佛罗里达奇尔顿 43643

MS 乔克托 13859

克拉克女士 25833

FL 粘土 13932

佛罗里达州克利本 14972

FL 咖啡 49948

科尔伯特女士 54428

FL 科内库 13228

佛罗里达库萨 11539

卡温顿女士 37765

克伦肖女士 13906

佛罗里达卡尔曼 80406

佛罗里达戴尔 50251

佛罗里达达拉斯 43820

佛罗里达迪卡尔布 71109

埃尔莫尔女士 79303

MS Escambia 38319

FL 埃托瓦 104430

FL 费耶特 17241

富兰克林女士 31704

FL 日内瓦 26790

格林 9045 号女士

佛罗里达州黑尔 15760

FL 亨利 17302

佛罗里达州休斯顿 101547

jackson 女士 53227

杰斐逊女士 658466

FL 拉马尔 14564

劳德代尔号 92709

劳伦斯女士 34339

李女士 140247

MS 石灰石 82782

佛罗里达朗兹 11299

佛罗里达梅肯 21452

麦迪逊女士 334811

佛罗里达马伦戈 21027

佛罗里达马里昂 30776

编码(marshal)女士 93019

佛罗里达手机 412992

门罗女士 23068

FL 蒙哥马利 229363

FL 摩根 119490

佩里女士 10591

MS 皮肯斯 19746

佛罗里达派克 32899

佛罗里达伦道夫 22913

罗素 52947

佛罗里达州圣克莱尔 83593

FL 谢尔比 195085

萨姆特号 13763

佛罗里达塔拉迪加 82291

MS 塔拉普萨 41616

塔斯卡卢萨号 194656

佛罗里达沃克 67023

华盛顿女士 17581

威尔科克斯女士 11670

佛罗里达温斯顿 24484

最佳答案

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

while (std::getline(in >> state >> std::ws, county, '\t') >> population) {
    // ...
}

关于c++ - 我似乎无法得到正确的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37517056/

相关文章:

c++ - 以多态方式处理非多态对象,没有性能开销

c++ - 我什么时候应该防止隐式破坏?它是如何工作的?

c++ - 如何修复 "reference cannot be bound to dereferenced null pointer"警告

c++ - 未命中断点 - "the module did not load at the default load address"

c++ - 如何将 std::string_view 转换为 const char*?

c++ - 在 C : How to properly write some sort of "delete/dispose" function? 中使用 C++ 类

c++ - 在 replyFinished() 中获取发起 QNetworkRequest 的函数

c++ - 什么是适用于 Windows 7 和 .NET Framework 3.5 SP1 的 Microsoft Windows SDK?

c++ - Mac 上的 OpenMP 不再工作

c++ - 使用 -Wall 而不是 -o 时出现 "multiple definition of _start"错误