本文介绍了第一个fstream程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个从这个文本文件获取数据的程序:



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



它还必须包含以下五个函数:



openFiles :this函数打开输入和输出文件,并将浮点数的输出设置为具有小数点和尾部零的固定小数格式的两位小数。



:此函数查找女生和男生GPA的总和。 / p>

平均成绩:此功能可查找男女学生的平均GPA。



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



我认为我已经编写了很好的函数,我的第一个程序使用fstream im不知道我需要在我的主要功能中实现他们。



这里是我到目前为止:



标题

  #ifndef header_h 
#define header_h

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

using namespace std;

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



#endif

::

  #includeheader.h


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

系统(PAUSE);
return EXIT_SUCCESS;
}

openFiles

  #includeheader.h

void openFiles(ifstream inData,ofstream outData)
{

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

outData<<固定<显示点<< setprecision(2);

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

}

sumGrades

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

averageGrade

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

printResults

  #includeheader.h

void
{
outData< 平均男性GPA:< maleGrade<< endl;
outData<< 平均女性GPA:< femaleGrade<< endl;
}


解决方案


您不需要 averageGrade static_cast c>。



printResults 缺少其大部分功能签名。



sumGrades gpa gender 应该是局部变量,而不是参数。您还需要比较字符文字'm''f',而不是名为 m f 与随机内容。



流应始终传递





有问题吗?


I am trying to write a program that gets data from this text file:

Then, based on this data, the program is supposed to calculate the average gpa of each gender (f=female, m=male) and output the results in a new file.

It must also contain these five functions:

openFiles: this function opens the input and output files and sets the output of floating-point numbers to two decimal places in a fixed decimal format with a decimal point and trailing zeros.

initialize: this function initializes variables.

sumGrades: This function finds the sum of the female and male students GPAs.

average grade: This function finds the average GPA for male and female students.

printResults: this function outputs the relevent results.

I think I've done pretty good coding up the functions and all but since this is my first program using fstream im not sure how i need to implement them in my main function.

Here is what i have so far:

header:

#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

main:

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

openFiles:

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

averageGrade:

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

printResults:

#include "header.h"

void
{
     outData << "average male GPA: " << maleGrade << endl;
     outData << "average female GPA: " << femaleGrade << endl;
}
解决方案

You're making good progress.

You don't need the static_casts in averageGrade.

printResults is missing most of its function signature.

In sumGrades, gpa and gender should be local variables, not parameters. You also want to compare to the character literals 'm' and 'f', not variables named m and f with random contents.

Streams should ALWAYS be passed by reference, they can't be copied.

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

Did you have a question?

这篇关于第一个fstream程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 03:03