I am posting this problem because I am a newbie to C++ and this program has me totally stumped. Any direction, advice, examples would be extremely helpful! I will try to explain my problem as well as possible-

For research purposes and to better help students, the admission office of your local university wants to know how well female and male students perform in certain courses. You will receive a file that contains female and male students GPA's for certian courses. Due to confidentiality, the letter code f is used for female students and m is used for male students. Every file entry consists of a letter code followed by a GPA. Each line has one entry. The number of entries in the file is unknown. Write a program that computes and outputs the average GPA for both female and male students. Format your results to two decimal places. Your program should use the following functions.

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

b. Function initialize: This function initializes variables such as countFemale, countMale, sumFemaleGPA, and sumMaleGPA.

c. Function sumGrades: This function finds the sum of the female and male students GPA's.

d. Function averageGrade: This function finds the average GPA for female and male students.

e. Function printResults: This function outputs the relevant results.

f. There can be no global variables. Use the appropriate parameters to pass information in and out of functions,

here is my data file:
f 3.40
f 4.00
m 3.56
m 3.80
f 2.30
f 3.95
m 3.90
m 4.00
m 2.00
f 4.00
f 2.80
m 3.70
m 2.98
f 3.89
m 4.00
f 3.90
m 1.90
m 2.90
f 1.50
f 2.67
m 3.80
m 2.35
f 2.90
f 3.70
f 4.00
m 3.78
m 4.00
f 3.98

And here is the code I have so far:

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

using namespace std;

void openFiles();
void averageGpa();
void initialize();
void sumGrades();
void printResult(ofstream& outp, char gender, double avg);


main()
{
      int numMale=0;
      int numFemale=0;
      double averageFemale= 0.00;
      double averageMale = 0.00;
      double gpa = 0.00;
      double sumGpaFemale = 0.00;
      double sumGpaMale = 0.00;
      char gender;
      openFiles();
      cout << "Processing grades "<< endl;
      cout << "Output data is in file Ch7_Ex4Data.txt " << endl;
      cout << " "<< endl;
      system("pause");
      return 0;
      }
void openFiles()
{
      int numMale=0;
      int numFemale=0;
      double averageFemale= 0.00;
      double averageMale = 0.00;
      double gpa = 0.00;
      double sumGpaFemale = 0.00;
      double sumGpaMale = 0.00;
      char gender;
      ifstream infile;
      ofstream outfile;

      infile.open ("c:\\ch7_Ex4data.txt");
      if (!infile)
    {
        cout << "InFile not found" << endl;
        cout << "Program cannot execute." << endl;
        
    }
    


      outfile.open ("c:\\ch7_Ex4out.txt");

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

      infile >> gender >> gpa;
      while (infile >> gender >> gpa)
      {
            outfile << gender << " " << gpa << endl;
      }

     }

Here is my outfile so far (why does it change? It is missing the first line from the infile)
f 4.00
m 3.56
m 3.80
f 2.30
f 3.95
m 3.90
m 4.00
m 2.00
f 4.00
f 2.80
m 3.70
m 2.98
f 3.89
m 4.00
f 3.90
m 1.90
m 2.90
f 1.50
f 2.67
m 3.80
m 2.35
f 2.90
f 3.70
f 4.00
m 3.78
m 4.00
f 3.98

I know I am not going in the right direction! First when I try to output the data from the infile to the outfile the first and last numbers are missing in the outfile!
I know this code looks like a mess, and it is homework! So any advice or examples to lead me in the right direction would be greatly appreciated! I am not asking for someone to do the homework for me I just a better explanation as to the steps to take to complete this program.........Please any help will be beneficial, as i am not grasping the concepts very well
Thank you!!

Ok I solved the problem with the infile data missing the first line when sending to outfile. The line- infile >> gender >> gpa; before the while statement was making it skip a line. I removed that line and replaced the while statement with-

while (!infile.eof())
      {
            infile >> gender >> gpa;
            outfile << gender << " " << gpa << endl;

Ok I solved the problem with the infile data missing the first line when sending to outfile. The line- infile >> gender >> gpa; before the while statement was making it skip a line. I removed that line and replaced the while statement with-
while (!infile.eof())
{
infile >> gender >> gpa;
outfile << gender << " " << gpa << endl;

You fixed it the wrong way. All you had to do was delete line 60 and leave the rest alone. You had that loop coded correct, and replaced it with bad code.

The instructions say nothing about openFile() function reading the files. All that function is supposed to do is open the file, so you need to pass the ifstream and ofstream to that function by reference.

void openFile(ifstream& in, ofstream& out)
{

}

Nowhere in any of those instructions are you told when to read the input file, so I presume the input file is read in sumGrades() and again in averageGrade().

Thank you! I will change back. May I ask why the-
while (!infile.eof())
{
infile >> gender >> gpa;
outfile << gender << " " << gpa << endl;

is considered bad code??

Thank you! I will change back. May I ask why the-
while (!infile.eof())
{
infile >> gender >> gpa;
outfile << gender << " " << gpa << endl;

is considered bad code??

Because it doesn't work, that's why. eof() is known only AFTER an attempt to read after end-of-file. The construct you posted attempts to look-ahead and see if the next read will be eof(), which of course can't be done. Consequently the last line of the file will be processed twice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.