1 Replies - 138 Views - Last Post: 22 May 2019 - 10:45 AM Rate Topic: -----

#1 Bahaar Mubeen   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-May 19

Display Average cgpa for males and females

Posted 22 May 2019 - 10:08 AM

Attached Imagei can not get the average CGPA because the loop is displaying 0 for males and females even when the file was created. How to make the function outputResults return the correct output .


#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;
bool openFiles(ifstream&, ofstream&);
void initializeVariables(int&, int&, double&, double&);
void readAndSumGPAs(ifstream&, double&, double&, int&, int&);
void averageGPA(double, double, int, int, double*, double*);
void outputResults(ofstream&, int, double, int, double);
int main()
{
	ifstream fileOpening, fin;
	ofstream fout;
	if(fileOpening)
	{
		int countOfMale, countOfFemale;
		double sumOfMale, sumOfFemale;
		initializeVariables(countOfFemale, countOfMale, sumOfFemale, sumOfMale);
		readAndSumGPAs(fin, sumOfFemale, sumOfMale, countOfFemale, countOfMale);
		double averageOfFemale = 0, averageOfMale = 0;
		double*p = &averageOfFemale;                              // pointer for average of female variable
		double*q = &averageOfMale;                               // pointer for average of male variable 
		averageGPA(sumOfMale, sumOfFemale, countOfMale, countOfFemale, q, p);
		outputResults(fout, countOfMale, *q, countOfFemale, *p);
		fin.close();
		fout.close();
	}
	return 0;
}
bool openFiles(ifstream& fin, ofstream& fout)				 // Function for open input and output file
{
	fin.open("C:\\Users\\PC\\Desktop\\VS tasks\\assign me 1\\assign me 1\\cgpa.txt");
	fout << setprecision(2) << fixed << showpoint;//  open cgpa.txt input file
	fout.open("C:\\Users\\PC\\Desktop\\VS tasks\\assign me 1\\assign me 1\\summary.txt");                              //  open summary.txt output file
	if (fin && fout)
	{
		cout << " true open ";
		return true;
	}
	else
		cout << " false open ";
	return false;
}
void initializeVariables(int& female, int&male, double&sumFemale, double&sumMale)  // Function for intializing Variable
{
	female = 0;
	male = 0;
	sumFemale = 0;
	sumMale = 0;
}
void readAndSumGPAs(ifstream&fin, double&sumFemale, double&sumMale, int&female, int&male) //Function for read input file 
{
	char ch;
	double cgpa;
	while (fin)
	{
		fin >> ch;
		fin >> cgpa;
		if (ch == 'F')
		{
			female++;
			sumFemale += cgpa;
		}
		else if (ch == 'M')
		{
			male++;
			sumMale += cgpa;
		}
	}
}
void averageGPA(double sumMale, double sumFemale, int male, int female, double*q, double*p) // computing average
{
	*p = sumFemale / female;
	*q = sumMale / male;
}
void outputResults(ofstream&fout, int male, double avgMale, int female, double avgFemale)   // output Function
{
	cout  << "Average CGPA of " << male << " male students: " << avgMale << endl;
	cout  << "Average CGPA of " << female << " female students: " << avgFemale;
}


This post has been edited by Skydiver: 22 May 2019 - 12:39 PM
Reason for edit:: Fixed botched code tags. Thank you for trying!


Is This A Good Question/Topic? 0
  • +

Replies To: Display Average cgpa for males and females

#2 jimblumberg   User is offline

  • member icon

Reputation: 5731
  • View blog
  • Posts: 17,558
  • Joined: 25-December 09

Re: Display Average cgpa for males and females

Posted 22 May 2019 - 10:45 AM

Where do you ever open the files?

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1