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
|
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
struct players
{
char name[20], position[50];
int touchdowns, catches, passingYards,
receivingYards, runningYards;
};
void printmenu();
void printData(struct players s[], int size);
void searchNamestr(struct players s[], int size, string searchName);
int main()
{
int i,choice, notquit = 1;
ifstream infile;
string searchName;
bool found = false;
int index;
infile.open("footballPlayers.txt"); // step 3 - associate ifile with filename
struct players s[10];
for (i = 1; i < 10; i++)
{
infile >> s[i].name >> s[i].position >> s[i].touchdowns >> s[i].catches >> s[i].passingYards >>
s[i].receivingYards >> s[i].runningYards;
}
while (notquit)
{
printmenu();
cin >> choice;
switch (choice)
{
case 1:printData(s,10);
system("pause");
break;
case 2:
searchNamestr(s,10, searchName );
system("pause");
break;
case 3:
system("pause");
break;
case 0: notquit = 0;
break;
default:
cout << "Wrong entry, please try again" << endl;
system("pause");
}
}
infile.close(); // step 5 - close input file
//ofile.close(); // step 5 - close output file
system("pause");
return 0;
}
void printmenu()
{
system("CLS");
cout << "*******Please select your option*******" << endl;
cout << "\n1. Display Player's Information ";
cout << "\n2. Search a Player";
cout << "\n3. Update a Player's Information";
cout << "\n0. Exit" << endl;
cout << "\nSelection: ";
}
void printData(struct players s[], int size)
{
int i;
for (i = 1; i<10; i++)
{
cout << "--------------------------------";
cout << "\nName: " << s[i].name;
cout << "\nPosition: " << s[i].position;
cout << "\nTouchdowns: " << s[i].touchdowns;
cout << "\nCatches: " << s[i].catches;
cout << "\nPassing Yards: " << s[i].passingYards;
cout << "\nReceiving Yards: " << s[i].receivingYards;
cout << "\nRunning Yards: " << s[i].runningYards;
cout << "\n------------------------------" << endl;
}
}
void searchNamestr(struct players s[], int size, string searchName)
{
cout << "Enter the name of the player: ";
cin >> searchName;
int flag = 0; // set flag to off
for (int i = 1; i < 10; i++) // start to loop through the array
{
if (s[i].name == searchName) // if match is found
{
flag = 1; // turn flag on
break; // break out of for loop
}
}
for (int i = 0; i<10; i++)
if (flag) // if flag is TRUE (1)
{
cout << "The number of touchdowns " << s[i].touchdowns << ".\n" << "The Number of catches: " << s[i].catches << endl;
}
else
{
cout << "Sorry, I could not find that player in this array." << endl << endl;
}
}
|