Trying to search array for user input

Greetings. I am getting 2 errors that I cannot reconcile.

ERROR C2664 'int winnersArray(std::string)': cannot convert argument 1 from 'std::string [120]' to 'std::string' line 29
ERROR C2660 'findWinners': function does not take 3 arguments line 39

The 2 referenced .txt files are inside the appropriate project folder.

I am trying to create 3 functions to call into my main: one function, displayTeams reads the Teams.txt file and displays the names for the user; another function reads the contents of WorldSeriesWinners.txt into an array; lastly, another function is supposed to search the previously created array for the user input and counts how many time it is found.

I believe my problem is in my function findWinners (lines 83-95), but I can't figure it out. Can someone help me, please?

Thank you in advance for taking the time to look at my code.



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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstring>
using namespace std;

// Function prototypes 
void displayTeams(string);
int findWinners(string);
int winnersArray(string);

// Main function
int main()
{
	// Constant for array size
	const int maxTeams = 120;

	// Declare array
	string teamArray[maxTeams];

	// Declare variable for input
	string teamName;

	// Declare variables for info processing
	int wins, numTeamsRead;

	// Building array
	numTeamsRead = winnersArray(teamArray);

	// Display team names
	displayTeams("Teams.txt");

	// Reading team name
	cout << "\n\nEnter the name of one of the teams: ";
	getline(cin, teamName);

	// Finding number of wins
	wins = findWinners(teamArray, numTeamsRead, teamName);

	// Printing result
	cout << "\n\nThe " << teamName << " have won the World Series " << wins << " time(s) between 1903 and 2012\n";

	system("pause");
	return 0;
}

// Functions used in main

// Function to build array
int winnersArray(string [])
{
	const int ARRAY_SIZE = 120;  // Array size
	int teamArray[ARRAY_SIZE];	 // Array with 120 elements
	int count2 = 0;              // Loop counter variable
	ifstream inputFile;          // Input file stream object
	
	// Open the file
	inputFile.open("WorldSeriesWinners.txt"); 

	if (inputFile)
	{
		// Read the team names from the file into the array
		// After this loop executes, the count variable will hold
		// the number of values that were stored in the array
		while (count2 < ARRAY_SIZE && inputFile >> teamArray[count2])
			count2++;
	}
	else
	{
		// Display an error message.
		cout << "Error opening the file.\n";

		// Exit the program.
		exit(0);

	}
	// Close the file
	inputFile.close();
	return count2;
}
// Function to search array
int findWinners(string teamArray[], int numTeams, string searchTeams)
{
	int index3, count3 = 0;

	// Loop over array
	for (index3 = 0; index3 < numTeams; index3++)
	{
		// Compare team names
		if (strcmp((teamArray[index3]).c_str(), searchTeams.c_str()) == 0)
			count3++;
	}
	return count3;
}

// Function to display team names

// ********************************************************
// The displayTeams function reads data from a file and   *
// displays it on the screen.                             *
// ********************************************************
void displayTeams(string filename)
{
	string data;   // To hold the data from the file
	int count1 = 0; // Counter variable for the columns

	// File stream object to read data from a file
	ifstream inputFile;
	// Open the file.
	inputFile.open(filename);
	// If the file successfully opened, process it.
	if (inputFile)
	{
		// Display a description of the file's contents.
		cout << "The following teams have won "
			<< "the World Series at least once:\n\n";
		while (getline(inputFile, data))
		{
			// Format output to be left-aligned
			// with a field width of 24 characters.
			cout << left << setw(24);
			// Display an item from the file.
			cout << data;
			// Increment the column counter variable.
			count1++;
			// Determine if three columns have been displayed.
			if (count1 >= 3)
			{
				// If so, end the line and begin a new row of output.
				cout << endl;
				// Reset the column counter variable to zero.
				count1 = 0;
			}
		}
		// Set the alignment back to the default setting.
		cout << right << endl;
		
		// Added for best programming practice
		// Close the file
		inputFile.close();
	}
	else
	{
		// Display an error message.
		cout << "Error opening the file.\n";

		// Exit the program.
		exit(0);
	}
}
It looks like you defined the function prototypes to take a single string but your function implementations and your function call is using an array of string.

Indeed.


Once you get past that, you will hit a logical issue. Where does the winnersArray() store data that it reads from a file?

Do you really want to exit(0) on line 75?
If you don't, then the function returns 0 as "number of winners" (I presume) and the main() could handle that situation.
Furthermore, returning 0 implies success. Is the failure to read input really a success?


What does "find" or "search" mean? In your findWinners() it apparently means counting how many times a specific word occurs in an array.

Furthermore, std::string has operator==. There is no reason to call strcmp().
In fact, the entire findWinners() could be replaced with a call to Standard Library algorithm std::count.
http://www.cplusplus.com/reference/algorithm/count/
Thank you.

jlb: How do I reconcile my function prototypes to align with their implementations?

keskiverto: Whew... that's a lot for me to take in.

The exit(0) on line 75 should only occur if the requested file doesn't open, I think, but I see your logic. How can I improve the case of the file not opening?

You are correct, I want to count how many time the user input occurs in the built array.

I will have to read about std::count.
How do I reconcile my function prototypes to align with their implementations?


The easiest way is to replace your current prototype with the implementation header.

Instead of int findWinners(string); use int findWinners(string teamArray[], int numTeams, string searchTeams);

You should get into the habit of actually naming the parameters in the prototypes to help document the purpose of the arguments.

Last edited on
I have made some changes, but I still cannot count the number of times that a team appears in my array. Can I get some more help with my countWinners() function?

I renamed findWinners() to countWinners().

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

using namespace std;

// Global constant
const int ARRAY_SIZE = 120;

// Function prototypes 
void displayTeams(string);
void countWinners(string teamArray[], string searchTeams);
void winnersArray(string[]);

// Main function
int main()
{
	// Constant for array size
	const int maxTeams = 120;

	// Declare array
	string teamArray[maxTeams];

	// Declare variable for input
	string teamName;

	// Building array
	winnersArray(teamArray);

	// Display team names
	displayTeams("Teams.txt");

	// Reading team name
	cout << "\n\nEnter the name of one of the teams: ";
	getline(cin, teamName);

	// Finding number of wins
	countWinners(teamArray, teamName);

	system("pause");
	return 0;
}

// Functions used in main

// Function to build array
// Based on Program 7-14 Gaddis text
void winnersArray(string [])
{
	string teamArray[ARRAY_SIZE];	 // Array with 120 elements
	int count2 = 0;                  // Loop counter variable
	ifstream inputFile;              // Input file stream object
	string data;				     // Variable to hold data from the file

	// Open the file
	inputFile.open("WorldSeriesWinners.txt"); 

	if (inputFile)
	{
		// Read the team names from the file into the array
		// After this loop executes, the count2 variable will hold
		// the number of values that were stored in the array
		while (getline(inputFile, data))
		{
			teamArray[count2] = data;
			count2++;
		}
	}
	else
	{
		// Display an error message.
		cout << "Error opening WorldSeriesWinners.txt.\n";

		// Exit the program.
		system("pause");
		exit(0);

	}
	// Close the file
	inputFile.close();
	//return count2;
}
// Function to search array
void countWinners(string teamArray[], string searchTeams)
{
	int count3 = 0;

	// Loop over array
	for (int index3 = 0; index3 < ARRAY_SIZE; index3++)
	{
		// Compare team names
		if (teamArray[index3] == searchTeams)
		{
			count3++;
		}

	}
	// Printing result
	cout << "\n\nThe " << searchTeams << " have won the World Series " << count3 << " time(s) between 1903 and 2012.\n\n";
}

// Function to display team names
// Copied from D2L, supplied by instructor 

// ********************************************************
// The displayTeams function reads data from a file and   *
// displays it on the screen.                             *
// ********************************************************
void displayTeams(string filename)
{
	string data;   // To hold the data from the file
	int count1 = 0; // Counter variable for the columns

	// File stream object to read data from a file
	ifstream inputFile;
	// Open the file.
	inputFile.open(filename);
	// If the file successfully opened, process it.
	if (inputFile)
	{
		// Display a description of the file's contents.
		cout << "The following teams have won "
			<< "the World Series at least once:\n\n";
		while (getline(inputFile, data))
		{
			// Format output to be left-aligned
			// with a field width of 24 characters.
			cout << left << setw(24);
			// Display an item from the file.
			cout << data;
			// Increment the column counter variable.
			count1++;
			// Determine if three columns have been displayed.
			if (count1 >= 3)
			{
				// If so, end the line and begin a new row of output.
				cout << endl;
				// Reset the column counter variable to zero.
				count1 = 0;
			}
		}
		// Set the alignment back to the default setting.
		cout << right << endl;
		
		// Added for best programming practice
		// Close the file
		inputFile.close();
	}
	else
	{
		// Display an error message.
		cout << "Error opening Teams.txt.\n";

		// Exit the program.
		system("pause");
		exit(0);
	}
}
Look at this snippet:
1
2
3
// Function to build array
// Based on Program 7-14 Gaddis text
void winnersArray(string [])


Do you realize that this function implementation is not using the parameter because the parameter has no name? This means that this function is actually accomplishing nothing.

By the way you really should avoid the exit() function in a C++ program. This C function doesn't understand C++ classes and can lead to data corruption.



Topic archived. No new replies allowed.