0

Much better, try to fix the last few lines in your main() too. Anyhow, so, what was the problem besides your formatting???

0

It runs but nothing appears correctly in the output file.

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;
void OpenFiles(char ch, float gpa);
void initialize(int countFemale, int countMale, float sumFemGPA, float sumMaleGPA);
void sumGrades(int countFemale, int countMale, float sumMaleGPA, float sumFemGPA);
void averageGrades(float avgFemGPA, float avgMaleGPA);
void printResults(int countFemale, int countMale, float avgMaleGPA, float avgFemGPA, float sumMaleGPA, float sumFemGPA);

int main ()
{
    ifstream inFile;
    ofstream outFile;
    char ch;
    float gpa;
    float avgFemGPA, avgMaleGPA;
    int countFemale, countMale;
    float FemGPA, MaleGPA, sumFemGPA, sumMaleGPA;

initialize(countFemale, countMale, sumFemGPA, sumMaleGPA);

OpenFiles(ch, gpa);

while(!inFile.eof())
    {
        sumGrades(countFemale, countMale, sumMaleGPA, sumFemGPA);
        averageGrades(avgFemGPA, avgMaleGPA);
        inFile >> ch >> gpa;
    }
  cout << "Processing grades" << endl;
  cout << "Output data is in file Ch7_Ex4Data.txt" << endl;

    printResults(countFemale, countMale, avgMaleGPA, avgFemGPA, sumMaleGPA, sumFemGPA);
    inFile.close();
    outFile.close();
    system ("pause");

return 0;
}

void OpenFiles(char ch, float gpa)
{
    ifstream inFile;
    ofstream outFile;
    inFile.open ("Ch7_Ex4Data.txt");
    outFile.open ("Ch7_Ex4Out.txt");
    if(!inFile)
    {
        cout << "Cannot open input file." << endl;
    }
    inFile.get(ch);
    inFile >> gpa;
    inFile.eof();

    outFile << fixed << showpoint;
    outFile << setprecision(2);
}

void initialize(int countFemale, int countMale, float sumFemGPA, float sumMaleGPA)

{
    float FemGPA = 0.0;
    float MaleGPA = 0.0;
    countFemale = 0;
    countMale = 0;
}

void sumGrades(int countFem, int countMale, float sumMaleGPA, float sumFemGPA)
{
    char ch;
    float gpa;
    OpenFiles(ch, gpa);
    float MaleGPA, avgMaleGPA, FemGPA, avgFemGPA;
    switch (ch)
    {
        case 'M':
        case 'm': MaleGPA = MaleGPA + gpa;
        countMale++;
        avgMaleGPA = MaleGPA / countMale;
        break;
        case 'F':
        case 'f': FemGPA = FemGPA + gpa;
        countFem++;
        avgFemGPA = FemGPA / countFem;
        break;
    }
}

void averageGrades(float avgFemGPA, float avgMaleGPA)
{

    float FemGPA, MaleGPA;
    int countFem, countMale;
    float sumMaleGPA, sumFemGPA;
    sumGrades (countFem, countMale, sumMaleGPA, sumFemGPA);
    avgFemGPA = FemGPA / countFem;
    avgMaleGPA = MaleGPA / countMale;

}

void printResults(int countFemale, int countMale, float avgMaleGPA, float avgFemGPA, float sumMaleGPA, float sumFemGPA)
{
    ofstream outFile;
    outFile << "Number of female students = " << countFemale << endl;
    outFile << "Average female GPA = " << avgFemGPA << endl;
    outFile << "Number of male students = " << countMale << endl;
    outFile << "Average male GPA = " << avgMaleGPA << endl;
}
0

Let me ask you something? How did you learn C++, as I observe really essential mistakes in your code!

For example, you have a function called "initialize", and it is SUPPOSED to initialize some values, and it DOES, but its own LOCAL variables. You need to send your variables by reference to do it the way you did it.

Or multiple opening of files which is not needed! Why are you using fstream in your main() ????

Dude, before you do any coding, I suggest you to do read some C++ books. I suggest you to start by Complete C++ reference(link to amazon is here) as I did find it helpful and easy to refer to parts you need. or if you need some much more detailed, "C++ How to program" is not a bad book to start with(link here).

0

Re: Formatting. It's very important. Get Visual C++ and let it format for you. Let it "detabify" for you too and everyone's happy.

Regarding code like this:

void initialize(int countFemale, int countMale, float sumFemGPA, float sumMaleGPA)
{
    float FemGPA = 0.0;
    float MaleGPA = 0.0;
    countFemale = 0;
    countMale = 0;
}

you need to do two things. Research "global" versus "local" variables and "pass by reference" versus "pass by value". For this particular function, it has to be reference and you need to get rid of those local variables FemGPA and MaleGPA, which do nothing, and instead use the parameters you passed:

void initialize(int& countFemale, int& countMale, float& sumFemGPA, float& sumMaleGPA)
{
    sumFemGPA = 0.0;
    sumMaleGPA = 0.0;
    countFemale = 0;
    countMale = 0;
}

& stands for "pass by reference" here.

But I think you need to take several steps back and look at the whole program. It can't be done correctly if you don't understand global versus local variables and pass by reference versus pass by value, so I'd put the assignment aside completely and google some tutorials on those topics, then when you understand them, revisit your program.

0

It will run now but when I open the output file it outputs: Number of female students = 12
Average female GPA = 206323346029811400000000000.00
Number of male students = 14
Average male GPA = 0.29
any ideas on why it may be doing this? The avg female gpa should be around 3.36 and there are 14 female students & 14 male students. I haven't calculated what male gpa really should be yet.

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;
void OpenFiles(ifstream& inFile, ofstream& outFile);
void initialize(int& countFemale, int& countMale, float& sumFemGPA, float& sumMaleGPA);
void sumGrades(int& countFemale, int& countMale, float& sumMaleGPA, float& sumFemGPA, ifstream& inFile, ofstream& outFile, char ch, double gpa);
void averageGrades(int& countFemale, int& countMale, float& sumFemGPA, float& sumMaleGPA, float& avgFemGPA, float& avgMaleGPA);
void printResults(ifstream& inFile, ofstream& outFile, int& countFemale, int& countMale, float& avgMaleGPA, float& avgFemGPA, float& sumMaleGPA, float& sumFemGPA);

int main ()
{
    ifstream inFile;
    ofstream outFile;
    char ch;
    float gpa;
    float avgFemGPA, avgMaleGPA;
    int countFemale, countMale;
    float FemGPA, MaleGPA, sumFemGPA, sumMaleGPA;

initialize(countFemale, countMale, sumFemGPA, sumMaleGPA);

OpenFiles(inFile, outFile);

while(!inFile.eof())
    {
        sumGrades(countFemale, countMale, sumMaleGPA, sumFemGPA, inFile, outFile, ch, gpa);
        averageGrades(countFemale, countMale, sumMaleGPA, sumFemGPA, avgFemGPA, avgMaleGPA);
        inFile >> ch >> gpa;
    }
  cout << "Processing grades" << endl;
  cout << "Output data is in file Ch7_Ex4Out.txt" << endl;

    printResults(inFile, outFile, countFemale, countMale, avgMaleGPA, avgFemGPA, sumMaleGPA, sumFemGPA);
    inFile.close();
    outFile.close();
    system ("pause");

return 0;
}

void OpenFiles(ifstream& inFile, ofstream& outFile)
{
    char ch;
    double gpa;
    inFile;
    outFile;
    inFile.open ("Ch7_Ex4Data.txt");
    outFile.open ("Ch7_Ex4Out.txt");
    if(!inFile)
    {
        cout << "Cannot open input file." << endl;
    }
    inFile.get(ch);
    inFile >> gpa;
    inFile.eof();

    outFile << fixed << showpoint;
    outFile << setprecision(2);
}

void initialize(int& countFemale, int& countMale, float& sumFemGPA, float& sumMaleGPA)

{
    sumFemGPA = 0.0;
    sumMaleGPA = 0.0;
    countFemale = 0;
    countMale = 0;
}

void sumGrades(int& countFemale, int& countMale, float& sumMaleGPA, float& sumFemGPA, ifstream& inFile, ofstream& outFile, char ch, double gpa)
{
    ch;
    gpa;
    float MaleGPA, avgMaleGPA, FemGPA, avgFemGPA;
    switch (ch)
    {
        case 'M':
        case 'm': MaleGPA = MaleGPA + gpa;
        countMale++;
        avgMaleGPA = MaleGPA / countMale;
        break;
        case 'F':
        case 'f': FemGPA = FemGPA + gpa;
        countFemale++;
        avgFemGPA = FemGPA / countFemale;
        break;
    }
}

void averageGrades(int& countFemale, int& countMale, float& sumFemGPA, float& sumMaleGPA, float& avgFemGPA, float& avgMaleGPA)
{

    float FemGPA, MaleGPA;
    countFemale; 
    countMale;
    sumMaleGPA;
    sumFemGPA;
    avgFemGPA = FemGPA / countFemale;
    avgMaleGPA = MaleGPA / countMale;

}

void printResults(ifstream& inFile, ofstream& outFile, int& countFemale, int& countMale, float& avgMaleGPA, float& avgFemGPA, float& sumMaleGPA, float& sumFemGPA)
{
    outFile;
    outFile << "Number of female students = " << countFemale << endl;
    outFile << "Average female GPA = " << avgFemGPA << endl;
    outFile << "Number of male students = " << countMale << endl;
    outFile << "Average male GPA = " << avgMaleGPA << endl;
}
0

Line 57 is meaningless. Delete it. Lines 72 - 90 : Why are you averaging in a function that is supposed to calculate the sum?

Line 72 : Either use inFile and outFile in the function or don't pass them. Either use sumMaleGPA and sumFemGPA or don't pass them.

Line 76 : What do MaleGPA and FemGPA represent? You already have gpa, avgFemGPA, avgMaleGPA, sumFemGPA, sumMaleGPA. You use them without initializing them, but unless there is a need for them, get rid of them altogether.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.