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
|
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
struct playerinfo
{
string playernum, playername, playerpos, playercol;
int height, weight, age, exp;
};
playerinfo changeStats(playerinfo team[], int member, string playerData[])
{
//access specific player and alter stats
for (int i=0; i<8; i++)
{
if (i==0)
team[member].playernum=playerData[i];
else if (i==1)
team[member].playername=playerData[i];
else if (i==2)
team[member].playerpos=playerData[i];
else if (i==3 || i==4 || i==5 || i==7)
{
//convert to int before adding to structure when applicable
int x;//int to hold converted values
stringstream convertor;//used for conversion
convertor<<playerData[i];//reads in strings in playerData
convertor>>x;//adds converted value to x
if (i==3)
team[member].height=x;
else if (i==4)
team[member].weight=x;
else if (i==5)
team[member].age=x;
else
team[member].exp=x;
}
else
team[member].playercol=playerData[i];
}
return team[member];//return modified team member
};
int main()
{
string playerData[8];//used to store 8 values for each player
playerinfo team[53];//declare players and initialize stats
fstream input;
input.open("packers.dat");//load file in
if (!input)
{
cout<<"Error loading packers.dat!";
return 1;
}
else
{
string word;
//change player based on file data read in
for (int j=0; j<2; j++)
{
for (int i=0; i<8; i++)
{
getline(input,word);//grab each field and store in word
if (word=="")//skip blank line between player's stats
getline(input,word);
playerData[i]=word;//assign each word to playerData
}
team[j]=changeStats(team,j,playerData);
}
input.close();//close input file
//output both player's info to confirm change
for (int i=0; i<2; i++)
{
cout<<"Player Number:\t"<<team[i].playernum<<endl;
cout<<"Player Name:\t"<<team[i].playername<<endl;
cout<<"Player Pos:\t"<<team[i].playerpos<<endl;
cout<<"Player Height:\t"<<team[i].height<<endl;
cout<<"Player Weight:\t"<<team[i].weight<<endl;
cout<<"Player Age:\t"<<team[i].age<<endl;
cout<<"Player Col:\t"<<team[i].playercol<<endl;
cout<<"Player Exp:\t"<<team[i].exp<<endl<<endl;
}
return 0;
}
}
|
Player Number: 87
Player Name: Phil
Player Pos: Forward
Player Height: 60
Player Weight: 140
Player Age: 27
Player Col: Harvard
Player Exp: 3
Player Number: 30
Player Name: John
Player Pos: Fullback
Player Height: 60
Player Weight: 250
Player Age: 23
Player Col: Alabama
Player Exp: 8 |