Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: pls help me.........C++ programming  (Read 5079 times)

0 Members and 1 Guest are viewing this topic.

voodoo_13e

  • Guest
pls help me.........C++ programming
« on: August 31, 2008, 09:18:38 AM »
something is wrong in my program....why that it only outputs (invalid gender)
can someone help me fix this program........i really need to finish my homework today....
i really this program to run properly.....

this is the problem:





Rewrite the program developed below so that the function main is merely a collection of function calls. 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 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.

/* For research purposes and to better help students, the admissions office of your local university wants to know how well female and male students perform in certain courses. You receive a file that contains female and male student's GPA's for certain courses. Due to confidentiality, the letter code f is used for female students and m 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.
*/

#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>

void main()
{
        ifstream inData;
        char gender;
        float average_gpa, sum_gpa = 0, gpa;
        int count = 0;

        inData.open("GPA_Detail.dat");
        if(!inData) {
                   cout<<"Cannot open input file. Program terminates!!!\n";
        }
 
        cout<<"Processing data...\n";
        inData>>gender>>gpa;
        count++;
        while(!inData.eof()) {
                  sum_gpa += gpa;
                  inData>>gender>>gpa;
                  count++;
        }

        average_gpa = sum_gpa / count;
        cout<<fixed;
        cout<<"The average gpa is " <<setprecision(2)    <<average_gpa<<".\n"
       
        inData.close();
}


########################################################
for now.....this is my progress..pls help me....




#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void OpenFiles(char ch, float gpa);
void initialize(int fcount, int mcount, float fgpa, float mgpa);
void sumGrades( int fcount, int mcount, float fgpa, float mgpa);
void averageGrades(float avfgpa, float avmgpa);
void printResults(int fcount, int mcount, float avfgpa, float avmgpa);
int main()

{
   ofstream out;
   ifstream in;
   char ch;
   float gpa;
   float avfgpa;
   float avmgpa;
   int fcount, mcount;
   float fgpa;  //define female GPA
   float mgpa;  //define male GPA

   initialize (fcount, mcount, fgpa, mgpa);

   OpenFiles(ch, gpa);

   while(!in.eof())
   {
      sumGrades(fgpa,mgpa,fcount,mcount);
      in>>ch>>gpa;
      averageGrades(avfgpa, avmgpa);

   }


   printResults(fcount, mcount, avfgpa, avmgpa);
   
}

void OpenFiles(char ch, float gpa)
{

   ofstream out;
   ifstream in;
   in.open("GPA_Detail.dat",ios::out | ios::ate);
   if (!in)
   {
      cout<<"Can not open input file"<<endl;
      cout<<"program terminates!!"<<endl;
   }
   in.get(ch);
   in>>gpa;
   in.eof();

   out.open("GPA_Detail.dat",ios::out | ios::ate);
   out<<fixed<<showpoint;
   out<<setprecision(2);
   //out<<"Female" <<ch<<endl;

}

void initialize(int fcount, int mcount, float fgpa, float mgpa)

{

   fgpa = 0.0;
   mgpa= 0.0;
   fcount = 0;
   mcount = 0;
}

void sumGrades(int fcount, int mcount, float fgpa, float mgpa)
{
   
   char ch;
   float gpa;
   OpenFiles(ch, gpa);
   switch (ch)
   {
      case 'F':
      case 'f': fgpa = fgpa+gpa;
         fcount++;
         //avfgpa = fgpa/fcount;
         break;
      case 'M':
      case 'm': mgpa = mgpa + gpa;
         mcount++;
         //avmgpa = mgpa/mcount;
         break;
      default: cout<<"invalid gender"<<endl;
         return;
   }
   
}

void averageGrades(float avfgpa, float avmgpa)
{
    
    float fgpa,  mgpa;
    int fcount, mcount;
    sumGrades(fcount, mcount, fgpa, mgpa);
    avfgpa = fgpa/fcount;
    avmgpa = mgpa/mcount;
}
   
void printResults(int fcount, int mcount, float avfgpa, float avmgpa)
{
   ofstream out;
   out<<"Number of female ="<<fcount<<endl;
   out<<"Average female GPA  = "<<avfgpa<<endl;
   out<<"Number of male ="<<mcount<<endl;
   out<<"Average male GPA ="<<avmgpa<<endl;
   out.close();
}