c++ - 第一个 fstream 程序

标签 c++

我正在尝试编写一个从该文本文件中获取数据的程序: alt text

然后,根据这些数据,程序应该计算每个性别(f=女性,m=男性)的平均 gpa,并将结果输出到一个新文件中。

它还必须包含这五个功能:

openFiles:此函数打开输入和输出文件,并以带小数点和尾随零的固定十进制格式将 float 的输出设置为两位小数。

初始化:这个函数初始化变量。

sumGrades:此函数计算男女学生 GPA 的总和。

平均成绩:此函数计算男女学生的平均 GPA。

printResults:该函数输出相关结果。

我认为我已经很好地编写了所有函数的代码,但由于这是我使用 fstream 的第一个程序,所以我不确定我需要如何在我的主要函数中实现它们。

这是我目前所拥有的:

标题:

#ifndef header_h
#define header_h

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>

using namespace std;

void extern initialize(int&, int&, float&, float&);
void extern openFiles(ifstream, ofstream);
void extern sumGrades(ifstream, ofstream, char, float, int&, int&, float&, float&);
void averageGrade (float&, float&, float, int, float, int);
void extern printResults (float, float, ofstream);



#endif

主要:

#include "header.h"


int main()
{
    char gender;
    float gpa, sumFemaleGPA, sumMaleGPA;
    ifstream inData;
    ofstream outData;
    int countFemale, countMale;



    inData.open("./Ch7_Ex4Data.txt");
    outData.open("./Ch7_Ex4Dataout.txt");

    do
    inData >> gender >> gpa;
    while(!inData.eof());




    inData.close();
    outData.close();

    system("PAUSE");
    return EXIT_SUCCESS;
}

打开文件:

#include "header.h"

void openFiles(ifstream inData, ofstream outData)
{

    inData.open("./Ch7_Ex4Data.txt");
    outData.open("./Ch7_Ex4Dataout.txt");

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

    inData.close();
    outData.close();

}    

sumGrades:

#include "header.h"

void sumGrades(ifstream inData, ofstream outData, char gender, float gpa, int& countFemale, int& countMale, float& sumFemaleGPA, 
               float& sumMaleGPA)
{
     char m, f;

    do
    {

     inData >> gender >> gpa;

           if(gender == m)
              {
                         sumMaleGPA += gpa;
                         countMale++;   
              }         

     else if (gender == f)
              {
                      sumFemaleGPA += gpa;
                      countFemale++;
              }
    }
    while(!inData.eof());
}                  

平均成绩:

#include "header.h"

void averageGrade (float& maleGrade, float& femaleGrade, float sumMaleGPA, int countMale, float sumFemaleGPA, int countFemale)
{
     maleGrade = sumMaleGPA / static_cast<float>(countMale);

     femaleGrade = sumFemaleGPA / static_cast<float>(countFemale);
}   

打印结果:

#include "header.h"

void
{
     outData << "average male GPA: " << maleGrade << endl;
     outData << "average female GPA: " << femaleGrade << endl;    
}     

最佳答案

你正在取得很好的进步。

您不需要 averageGrade 中的 static_cast

printResults 缺少其大部分函数签名。

sumGrades中,gpagender应该是局部变量,而不是参数。您还想与字 rune 字 'm''f' 进行比较,而不是名为 mf 的变量> 内容随机。

流应该始终通过引用传递,它们不能被复制。

When reading from a stream, you should test the stream itself in your while loop, not eof().

你有问题吗?

关于c++ - 第一个 fstream 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4479504/

相关文章:

c++ - 在字节数组中嵌入多个整数

c++ - 无法显示世界中的顶点

c++ - STL 容器 : Constructor's Allocator parameter and scoped allocators

c++ - Arm Compute Library - Canny Edge 从导入的 opencv 图像返回不可用的数据

c++ - 使用RDBMS来减少内存消耗?

C++ 使用 for 循环迭代 2 个 vector

c++ - int 变量不在 C++ 上重置?

c++ - 向 QT C++ 添加代码示例

c++ - native Nuget 包安装在 VS 中,但没有为 C++( native )项目添加引用

c# - C++ 与 C# 相比,计算速度提高了 15 倍,这是合法的吗?