World Series

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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>

using namespace std;

void getInput(string array[], ifstream&);
void displayTeams(string array[], int size);
void getChoice(int, int);
void countTimesWon(string array[], int size, string name);

int main()
{
		ifstream inputFile;
		int numTeams = 0,
		numGames = 0,
		teamWanted = 0,
		timesWon = 0;


		string teams[35];
		string winners[75];
		string teamName;


	inputFile.open("Teams.txt");
	if (inputFile.fail())
	{
		cout << "Error opening file teams.txt \n";

	}
	else
	{   numTeams = getInput(teams, inputFile);
		    inputFile.close();
	}

	inputFile.open("WorldSeries.txt");
	if (inputFile.fail())
	{
		cout << "Error opening file WorldSeries.txt  \n";
	}
	else
	{
		numGames = getInput(winners, inputFile);
		inputFile.close();
	}


		displayTeams(teams, numTeams);
		cout << "\nEnter the number of a team to learn how many"
			<< "\nWorld Series they have won between 1950 and 2014: ";
		teamWanted = getChoice(1, numTeams) - 1;
		teamName = teams[teamWanted];


		timesWon = countTimesWon(winners, numGames, teamName);
		cout << "\nThe " << teamName << " have won the World Series "
			<< timesWon << " times. \n";

		return 0;
}

int getInput(string A[], ifstream &inputFile)
{

	int pos = 0;

	while(getline(inputFile, A[pos]))
	{  pos++;
	}

	return pos;
}


void displayTeams(string A[], int numRecs)
{
	cout << "Major League Baseball Teams \n\n";
	for (int pos = 0; pos < numRecs; pos++)
		cout << setw(2) << pos+1 << ". " << A[pos] << endl;
}



int getChoice(int min, int max)
{
	int choice;

	cin >> choice;

	while (choice < min || choice > max)
	{
		cout << "Valid choices are" << min << "_" << max << endl;
		cout << "Please re-enter your team choice: ";
		cin >> choice;
	}
	return choice;
}

int countTimesWon(string A[], int numRecs, string name)
{
	int count = 0;
	
	for (int pos = 0; pos < numRecs; pos++)
	{
		if (A[pos] name)
			count++;
	}
	return count;
}
Last edited on
I am going to remind you yet again:

LEARN to use code tags, they make reading and commenting on source code MUCH easier.

How to use code tags: http://www.cplusplus.com/articles/jEywvCM9/

There are other tags available.

How to use tags: http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either

You continue to refuse to use code tags and people will start ignoring you.

One last chance from me, use tags.

Do NOT say the links do not work, I know for a fact they do. READ THEM!
Last edited on
ok there it took me aWHILE to understand how this website works
we don't have your input files, but:
numTeams = getInput(teams, inputFile); <--- getInput is a void function, no value is returned.
get choice is also a void function.

to return a value a function should have a type:
int add(int a, int b) {return a+b;} //ok, a+b resolves to int returned via the function int type.

you have
void add(int a, int b) {whatever;}
and say
x = add(40,2); //what is x?? add does not return anything, the assignment is nonsense.
Perhaps:

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;
}

Ill give this a try thank you!
Registered users can post here. Sign in or register to post.