Home › Forums › C Programming › c++ function help
- This topic has 1 reply, 2 voices, and was last updated 16 years, 1 month ago by Humayan.
- AuthorPosts
- August 31, 2008 at 5:56 am #2128ClaribelCondeParticipant123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102#include <iostream><br />#include <fstream><br />#include <iomanip><br />using namespace std;<br />void OpenFiles(char ch, float gpa);<br />void initialize(int fcout, int mcout, float fgpa, float mgpt);<br />void sumGrades( int fcout, int mcout, float fgpa, float mgpa);<br />void averageGrades(float avfgpa, float avmgpa);<br />void printResults(int fcout, int mcout, float avfgpa, float avmgpa);<br />void main()<br />{<br />ofstream out;<br />ifstream in;<br />char ch;<br />float gpa;<br />float avfgpa;<br />float avmgpa;<br />int fcout, mcout;<br />float fgpa; //define female GPA<br />float mgpa; //define male GPA<br />initialize (fcout, mcout, fgpa, mgpa);<br />OpenFiles(ch, gpa);<br />while(!in.eof())<br />{<br />sumGrades(fgpa,mgpa,fcout,mcout);<br />in>>ch>>gpa;<br />averageGrades(avfgpa, avmgpa);<br />}<br /><br />printResults(fcout, mcout, avfgpa, avmgpa);<br /><br />return;<br /><br />}<br />void OpenFiles(char ch, float gpa)<br />{<br />ofstream out;<br />ifstream in;<br />in.open("k:\prob-7.txt",ios::in);<br />if (!in)<br />{<br />cout<<"Can not open input file"<<endl;<br />cout<<"program terminates!!"<<endl;<br />}<br />in.get(ch);<br />in>>gpa;<br />in.eof();<br />out.open("k:\out.txt", ios::out);<br />out<<fixed<<showpoint;<br />out<<setprecision(2);<br />//out<<"Female" <<ch<<endl;<br />}<br />void initialize(int fcout, int mcout, float fgpa, float mgpa)<br />{<br />fgpa = 0.0;<br />mgpa= 0.0;<br />fcout = 0;<br />mcout = 0;<br />}<br />void sumGrades(int fcout, int mcout, float fgpa, float mgpa)<br />{<br /><br />char ch;<br />float gpa;<br />OpenFiles(ch, gpa);<br />switch (ch)<br />{<br />case 'F':<br />case 'f': fgpa = fgpa+gpa;<br />fcout++;<br />//avfgpa = fgpa/fcout;<br />break;<br />case 'M':<br />case 'm': mgpa = mgpa + gpa;<br />mcout++;<br />//avmgpa = mgpa/mcout;<br />break;<br />default: cout<<"invalid gender"<<endl;<br />return;<br />}<br /><br />}<br />void averageGrades(float avfgpa, float avmgpa)<br />{<br /><br />float fgpa, mgpa;<br />int fcout, mcout;<br />sumGrades(fcout, mcout, fgpa, mgpa);<br />avfgpa = fgpa/fcout;<br />avmgpa = mgpa/mcout;<br />}<br /><br />void printResults(int fcout, int mcout, float avfgpa, float avmgpa)<br />{<br />ofstream out;<br />out<<"Number of female ="<<fcout<<endl;<br />out<<"Average female GPA = "<<avfgpa<<endl;<br />out<<"Number of male ="<<mcout<<endl;<br />out<<"Average male GPA ="<<avmgpa<<endl;<br />out.close();<br />}<br /></iomanip></fstream></iostream>
give a lots of error vs warning, I think that having trouble with function calls, and passing to main function, but i have no idea to fix cause the book i am studying hasnt clear out much.summerize what program will do :
open a file with 2 columes; one is letter of f or m (represent for female and male), the other colume is GPA (grade).
read it and sum the gpa of female , sum the gpa of male, and display the everage of female and male gpa.
the function calls need to have 5 of them:
initialize variable
open and format output
sum gpa of female and male
find the average of female male gpa
print the rerults.
- August 31, 2008 at 11:07 am #3430HumayanParticipant
Could you give an example of what both the input and output files. The real problem with your program is how its organized and you haven’t used reference variables for some functions…..
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.