1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
//Project last
//Jessica Brown 7 Decemeber 2011
// Demonstrates the use of an array of student structures
// Reading, sorting, searching, etc.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct Studente
{
char geNder;
double score;
};
// Read all of the student objects from a text file into the array,
// one student at a time
int readStuArray( ifstream &inputFile, Studente everyone[], int maxSize )
{
Studente temp;
int count = 0;
// Loop while the array is not full, and there is a first name to read
// This assumes that if there is a first name, the rest of the data for
// an employee will be there also.
while ( count < maxSize && inputFile >> temp.geNder )
{
inputFile >> temp.score;
everyone[count] = temp;
count++;
} // while
return count;
} // readEmpArray()
// Display a single Student
void displayStu( const Studente &stu )
{
cout << setw(1) << stu.geNder;
cout << setw(5) << stu.score << endl;
} // displayStu()
// Display all of the studente objects in the array
void displayAll( const Studente everyone[], int size )
{
cout << " Data " << endl;
cout << "------" << endl;
for ( int i = 0; i < size; i++ )
displayStu( everyone[i] );
cout << endl << endl;
} // displayAll()
//another function for calculations
void displayStats( const Studente everyone[], int size )
{
int countm = 0;
int countf = 0;
double scorem = 0;
double scoref = 0;
for ( int i = 0; i < size; i++ )
{
if ( everyone[i].geNder == 'm' )
{
countm++;
scorem += everyone[i].score;
}//if
else if ( everyone[i].geNder == 'f' )
{
countf++;
scoref += everyone[i].score;
}//else if
}//for
cout << "Sum of male scores: " << scorem << endl;
cout << "Sum of female scores: " << scoref << endl;
cout << endl << endl;
cout << "Total male Count:" << countm << endl;
cout << "Total female Count:" << countf << endl;
cout << endl << endl;
cout << "Average male scores: " << (scorem /countm) << endl;
cout << "Average female scores: " << (scoref / countf) << endl;
cout << endl << endl;
} //displayStats
int main()
{
const int MAX_STU = 100;
ifstream inputFile( "Project3-input.txt" );
Studente everyone[MAX_STU];
int count;
if ( !inputFile )
{
cout << "Unable to open input file!" << endl;
system( "pause" );
exit( 1 );
}
// Read the Student objects from the file
count = readStuArray( inputFile, everyone, MAX_STU );
displayStats( everyone, count );
displayAll (everyone, count );
system( "pause" );
return( 0 );
} // main()
|