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
|
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
const int MaxTeam { 35 };
const int MaxWinners { 75 };
int getInput(std::string [], std::ifstream&, int);
void displayTeams(std::string [], int);
int getChoice(int, int);
int countTimesWon(const std::string [], int, const std::string&);
int main() {
std::string teams[MaxTeam];
std::string winners[MaxWinners];
std::ifstream inputFile("Teams.txt");
if (!inputFile)
return (std::cout << "Error opening file teams.txt \n"), 1;
const int numTeams { getInput(teams, inputFile, MaxTeam) };
inputFile.close();
inputFile.open("WorldSeries.txt");
if (!inputFile)
return (std::cout << "Error opening file WorldSeries.txt \n"), 2;
const int numGames { getInput(winners, inputFile, MaxWinners) };
displayTeams(teams, numTeams);
std::cout << "\nEnter the number of a team to learn how many"
<< "\nWorld Series they have won between 1950 and 2014: ";
const std::string teamName { teams[getChoice(1, numTeams) - 1] };
const int timesWon { countTimesWon(winners, numGames, teamName) };
std::cout << "\nThe " << teamName << " have won the World Series "
<< timesWon << " times. \n";
}
int getInput(std::string A[], std::ifstream& inputFile, int maxent) {
int pos {};
for (; pos < maxent && std::getline(inputFile, A[pos]); ++pos);
return pos;
}
void displayTeams(std::string A[], int numRecs) {
std::cout << "Major League Baseball Teams \n\n";
for (int pos {}; pos < numRecs; ++pos)
std::cout << std::setw(2) << pos + 1 << ". " << A[pos] << '\n';
}
int getChoice(int min, int max) {
int choice {};
do {
std::cin >> choice;
} while ((choice < min || choice > max) && (std::cout << "Valid choices are " << min << " - " << max << '\n' << "Please re-enter your team choice: "));
return choice;
}
int countTimesWon(const std::string A[], int numRecs, const std::string& name) {
int count {};
for (int pos {}; pos < numRecs; ++pos)
if (A[pos] == name)
++count;
return count;
}
|