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 131 132 133 134 135 136 137 138 139 140 141 142
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
ofstream out("Soccer.out");
const int PLAYERS=12; //Number of players on the roster
struct Info
{
string name;
int ID;
int goals;
};
//Function Prototypes
void Read(Info*, Info *);
void Print(Info*, Info*);
void Sorted(Info*, Info*);
void TotalTeamGoals(Info*, Info*);
Info MostGoals(Info*, Info*);
void Header();
void Read(Info *ptr, Info *spa)
{
ifstream inf("Soccer.in");
if(!inf)
{
cout << "error opening Soccer.in";
}
int n;
//initializes goals in Soccer struct array
for (ptr=spa; ptr < (spa+PLAYERS); ptr++)
{
(*ptr).goals = 0;
}
//reads through file and loads it into array
for (ptr=spa; ptr < (spa+PLAYERS); ptr++)
{
n = 0;
getline(inf, (*ptr).name);
inf >> (*ptr).ID;
inf.ignore(1);
while (inf.peek() != '\n' && inf.peek() != EOF)
{
inf >> n;
(*ptr).goals += n;
}
inf.ignore(1);
}
inf.close();
}
void Header()
{
out<<setw(40)<< "Soccer Statistics\n"
<<setw(10)<<"Name"
<<setw(25)<<" Player ID"
<<setw(35)<<" Total Goals goalsd"<<endl
<<"-------------------------------------"
<<"------------------------------------\n";
}
void Print(Info *ptr, Info *spa)
{
for(ptr=spa; ptr < (spa+PLAYERS); ptr++)
{
out << left << setw(14) << (*ptr).name << " "
<< right<< setw(16) << (*ptr).ID << " "
<< right << setw(28) << (*ptr).goals
<< endl;
}
out <<"-------------------------------------"
<<"------------------------------------\n";
}
void Sorted(Info *ptr, Info *spa)
{
Info temp;
for (int i = 0; i < PLAYERS; i++)
{
ptr=spa;
for (ptr = spa; ptr < (spa+(PLAYERS - 1)); ptr++)
{
if ((*ptr).name > (*(ptr + 1)).name)
{
temp = (*(ptr + 1));
(*(ptr + 1)) = *ptr;
*ptr = temp;
}
}
}
}
void TotalTeamGoals(Info *ptr, Info *spa)
{
int sum=0;
for(ptr=spa; ptr < (spa+PLAYERS); ptr++)
{
sum+=(*ptr).goals;
}
out << "Total Team Goals: "<< setw(43) << sum << endl;
out <<"-------------------------------------"
<<"------------------------------------\n\n";
}
Info MostGoals(Info *ptr, Info *spa)
{
int most = (*spa).goals; //highest goals
int MostID = 0; //id of highest scoring player
string MVP; //name of strongest player
for (ptr = spa; ptr < (spa+(PLAYERS-1)); ptr++)
{
if ((*ptr).goals > (*(ptr + 1)).goals)
{
most = (*ptr).goals;
MostID = (*ptr).ID;
MVP = (*ptr).name;
}
}
out << left << setw(20) << "MVP" << "ID#"
<< right << setw(19) << "goals" << endl
<<"---------------------------------------------\n"
<< left << setw(20) << MVP
<< MostID << right << setw(16) << most << endl;
}
int main()
{
Info *ptr = new Info[PLAYERS];
Info *spa = ptr;
Read(ptr,spa);
Header();
Sorted(ptr,spa);
Print(ptr, spa);
TotalTeamGoals(ptr,spa);
MostGoals(ptr,spa);
return 0;
}
|