Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Kevin Pavao
- //School: FAU
- //Class: ISM3230
- //Written: 11/5/06
- //purpose: input male and female student GPAs in a class and write the list and the average to a file
- //GenderGrades.cpp
- #include<iostream>
- #include<string>
- #include<fstream>
- #include<iomanip>
- using namespace std;
- //writes to the file chosen
- void input(int file);
- //averages the gpa
- double average(double gpa, double numGrades);
- //if invalid data entered, requests user to enter again
- void reEnter(char &resp, string type);
- //clears screen
- void clear();
- int main()
- {
- char resp='y';
- int file; //what file to open
- do
- {
- clear();
- //menu
- cout << "1. historyData.dat" << endl;
- cout << "2. politicalscienceData.dat" << endl;
- cout << "3. scienceData.dat" << endl;
- cout << endl << "Which file would you like to open? 1, 2 or 3: ";
- cin >> file;
- clear();
- input(file);
- cout << "File written.\n";
- cout << "Open another file? (y/n): ";
- cin >> resp;
- if(resp!='Y' && resp!='y' && resp!='n' && resp!='N') //if invalid char entered
- reEnter(resp,"continue");
- clear();
- }while((resp=='Y' || resp=='y') && (resp!='n' || resp!='N')); //continues until N or n is input
- return 0;
- }
- void input(int file)
- {
- ofstream outfile;
- char gender;
- char resp='y';
- double gpa, maleGPA=0.0, femaleGPA=0.0;
- int numMale=0, numFemale=0; //total number of males and females for averaging
- if(file==1)
- {
- outfile.open("historyData.dat");
- outfile << "Class: HST3239 History" << endl << endl;
- }
- if(file==2)
- {
- outfile.open("politicalscienceData.dat");
- outfile << "Class: POL5695 Political Science" << endl << endl;
- }
- if(file==3)
- {
- outfile.open("scienceData.dat");
- outfile << "Class: SCI3460 Science" << endl << endl;
- }
- //headings for the columns
- outfile << left << setw(10) << "Gender";
- outfile << right << setw(10) << "GPA" << endl;
- //continue until N or n is input
- while((resp=='Y' || resp=='y') && (resp!='n' || resp!='N'))
- {
- clear();
- cout << "Is the student male or female? (m/f): ";
- cin >> gender;
- if(gender!='M' && gender!='m' && gender!='F' && gender!='f')
- reEnter(gender,"gender");
- outfile << left << setw(10) << gender; //doesnt write to file until m or f is input
- cout << "Enter student GPA: ";
- cin >> gpa;
- outfile << setprecision(2) << setiosflags(ios::showpoint) << setiosflags(ios::fixed);
- outfile << right << setw(10) << gpa << endl; //write GPA to file with 2 decimal points
- if(gender=='M' || gender=='m')
- {
- maleGPA+=gpa; //adds gpa to total male gpa for averaging
- numMale++;
- }
- else
- {
- femaleGPA+=gpa; //adds gpa to total female gpa for averaging
- numFemale++;
- }
- clear();
- cout << "Is there another student? (y/n): ";
- cin >> resp;
- if(resp!='Y' && resp!='y' && resp!='n' && resp!='N')
- reEnter(resp, "continue");
- }
- outfile << setprecision(2) << setiosflags(ios::showpoint) << setiosflags(ios::fixed);
- //writes averages to file
- outfile << "\nNumber of males: " << numMale << endl;
- outfile << "Average male GPA: ";
- outfile << average(maleGPA,numMale) << endl << endl;
- outfile << "Number of females: " << numFemale << endl;
- outfile << "Average female GPA: ";
- outfile << average(femaleGPA,numFemale) << endl << endl;
- outfile << "Total average: ";
- outfile << average(femaleGPA+maleGPA,numFemale+numMale) << endl; //adds male and female GPA's
- outfile.close();
- }
- double average(double gpa, double numGrades)
- {
- double avg=gpa/numGrades;
- return avg;
- }
- void reEnter(char &resp, string type)
- {
- //if gender is not M or F
- if(type=="gender")
- {
- do
- {
- cout << "Invalid data entered, please enter M or F: ";
- cin >> resp;
- }while(resp!='M' && resp!='m' && resp!='F' && resp!='f');
- }
- //if value is not Y or N
- if(type=="continue")
- {
- do
- {
- cout << "Invalid data entered, continue? (y/n) ";
- cin >> resp;
- }while(resp!='Y' && resp!='y' && resp!='n' && resp!='N');
- }
- }
- void clear()
- {
- for(int x=0; x<=50; x++)
- cout << endl;
- }
Add Comment
Please, Sign In to add comment