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
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
#include <string.h>
#include <cstring>
using namespace std;
struct baseballPlayerdatabase
{
string firstname[20];
string lastname[20];
string playersteam[50];
int numberofhomers, runsbattedin;
double battingaverage;
};
void menu(ifstream& infile, baseballPlayerdatabase list[]); //brings out the menu
void getData(ifstream& infile, baseballPlayerdatabase list[]); //puts txt data into right structure members
void searchforaplayer(baseballPlayerdatabase list[]); //gets name,searches for their data,prints it out
void searchspecificteam(baseballPlayerdatabase list[]); //gets team, searches everything,prints anything relatable
void viewData(ifstream& infile, baseballPlayerdatabase list[]); //lets user see all baseball data
int main()
{
char response;
baseballPlayerdatabase list[10]; //declares the array
ifstream infile; // declares variable to read the file
infile.open("Playerdata.txt"); // opens txt file
getData(infile, list); // function to put txt data into structure-array members
cout << "Would you like to run the Baseball data program?(y/n):";
cin >> response;
cout << endl;
if (response == 'y') { menu(infile, list); }
else
{
cout << "Thanks for trying the baseball database program!" << endl;
}
return 0;
}
void menu(ifstream & infile, baseballPlayerdatabase list[])
{
int choice;
cout << "WELCOME TO THE BASEBALL MINI-DATABASE! - MAKE SELECTION BELOW!" << endl;
cout << "-------------------------------------------------------------" << endl;
cout << "1 - View existing player records \n";
cout << "2 - Search records by a player's last name \n";
cout << "3 - Search records by a team name \n";
cout << "4 - Update player record \n";
cout << "---------------------------------\n\n";
cin >> choice;
switch (choice)
{
case 1: { viewData(infile, list); break; }
case 2: { searchforaplayer(list); break; }
case 3: { searchspecificteam(list); break; }
}
}
void getData(ifstream& infile, baseballPlayerdatabase list[])
{
int i;
for (i = 0; i < 10; i++)
{
infile >> list[i].firstname
>> list[i].lastname
>> list[i].playersteam
>> list[i].numberofhomers
>> list[i].runsbattedin
>> list[i].battingaverage;
}
}
void viewData(ifstream& infile, baseballPlayerdatabase list[])
{
char response;
cout << " " << "Name" << " " << "Team" << " " << "Homers" << " " << "RBI" << " " << "BattingAverage" << endl;
cout << "-----------------------------------------------------------\n";
for (int x = 0; x < 10; x++)
{
cout << " " << list[x].firstname << " " << list[x].lastname << setw(17) << list[x].playersteam << setw(7) << list[x].numberofhomers << setw(9) << " " << list[x].runsbattedin << setw(7) << " " << list[x].battingaverage << endl;
}
cout << endl;
cout << "----------------------------------------" << endl;
cout << "Would you like to go back to the menu?(y/n)" << endl;
cin >> response;
if (response == 'y')
{
cout << "------------------------------------" << endl;
menu(infile, list);
}
else
{
cout << "Thanks for trying the baseball database program!" << endl;
}
}
void searchforaplayer(baseballPlayerdatabase list[])
{
string name;
int y;
cout << "Enter the last name of a player: ";
cin >> name;
for (y = 0; y < 10; y++)
{
if (list[y].lastname = name)
{
cout << list[y].lastname << list[y].playersteam << list[y].numberofhomers << list[y].runsbattedin << list[y].battingaverage << endl;
}
}
}
void searchspecificteam(baseballPlayerdatabase list[])
{
string name;
cout << "Enter the name of the team: ";
cin >> name;
if (list[1].playersteam = name)
{
cout << list[1].firstname << list[1].lastname << list[1].playersteam << list[1].numberofhomers << list[1].runsbattedin << list[1].battingaverage << endl;
}
}
|