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.
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cstring>
usingnamespace std;
// Function prototypes
void displayTeams(string);
int findWinners(string);
int winnersArray(string);
// Main function
int main()
{
// Constant for array size
constint 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 [])
{
constint 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.
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/
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 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?
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
usingnamespace std;
// Global constant
constint 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
constint 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);
}
}
// 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.