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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct player {
string name;
string position;
int touchdowns;
int catches;
int pass;
int receive;
int rush;
};
int addData( ifstream& ifile, int count , player football[])
{
if (count == 0)
{
ifile >> football[count].name;
ifile >> football[count].position;
ifile >> football[count].touchdowns;
ifile >> football[count].catches;
ifile >> football[count].pass;
ifile >> football[count].receive;
ifile >> football[count].rush;
count++;
}
else if (count != 0)
{
int i = 0;
string name;
string position;
int touchdowns;
int catches;
int pass;
int receive;
int rush;
while (i != -1)
{
ifile >> name >> position >> touchdowns >> catches >> pass >> receive >> rush;
if((*(&football + 1) - football) > 0)
{
if (name == football[i].name)
{
i++;
}
if (name != football[i].name)
{
football[count].name = name;
football[count].position = position;
football[count].touchdowns = touchdowns;
football[count].catches = catches;
football[count].pass = pass;
football[count].receive = receive;
football[count].rush = rush;
i = -1;
count++;
}
}
}
}
return count;
}
void display(int count, player football[])
{
cout << "There are " << count << " players," << endl;
if (count <= 0)
{
cout << "No players to display. ";
}
else
{
for (int i = 0; i < count;i++)
{
cout << i + 1 <<". " << football[i].name << endl;
cout << "Position: " << football[i].position << endl;
cout << "Touchdowns: " << football[i].touchdowns << endl;
cout << "Catches: " << football[i].catches << endl;
cout << "Passes: " << football[i].pass << endl;
cout << "Receiving Yards: " << football[i].receive << endl;
cout << "Rushing Yards: " << football[i].rush << endl;
}
}
}
void findandUpdate(string search, int count, player football[])
{
string name;
string position;
int touchdowns;
int catches;
int pass;
int receive;
int rush;
bool found = false;
for (int i = 0; i < count; i++)
{
if (search == football[i].name)
{
found = true;
cout << "Searched player is at index " << i + 1 << endl;
cout << "You may now update the player's name, player's position, number of touchdowns, number of catches,"
<< "number of passing yards, number of receiving yards, and the number of rushing yards \n";
cout << "Name: "; cin >> name;
cout << "Position: "; cin >> position;
cout << "Touchdowns: "; cin >> touchdowns;
cout << "Catches: "; cin >> catches;
cout << "Passes: "; cin >> pass;
cout << "Received: "; cin >> receive;
cout << "Rushed: "; cin >> rush;
football[i].name = name;
football[i].position = position;
football[i].touchdowns = touchdowns;
football[i].catches = catches;
football[i].pass = pass;
football[i].receive = receive;
football[i].rush = rush;
}
}
if (found == false)
{
cout << "No results.";
}
}
int main()
{
cout << "Press 1 to add a player.\nPress 2 to display players.\nPress 3 to find a player.\nPress 4 to update player information.\nPress 5 to save data.\n";
int x = 0;
cin >> x;
ifstream ifile;
player football[10];
ifile.open("footballplayers.txt");
int i = 0;
int count = 0;
while (i != -1)
{
switch (x)
{
case 1:
{
count = addData(ifile, count, football);
}
break;
case 2: display(count, football);
break;
case 3:
{
string name;
cout << "Enter football player name: ";
cin >> name;
findandUpdate(name,count, football);
}
break;
default: i = -1;
}
}
ifile.close();
string answer;
cout << "Save data? Type yes or no.";
cin >> answer;
if (answer == "yes")
{
ofstream ofile;
ofile.open("SavePlayerData.txt");
for (int i = 0; i < count; i++) {
ofile << football[i].name + " ";
ofile << football[i].name + " ";
ofile << football[i].position + " ";
ofile << football[i].touchdowns + " ";
ofile << football[i].catches + " ";
ofile << football[i].pass + " ";
ofile << football[i].receive + " ";
ofile << football[i].rush + " ";
ofile << "\n";
}
ofile.close();
}
}
|