Help!!! I can't get the right output

Hi all,

I'm doing a program which Ill post the criteria below. I need to get it to say how many world series they won but it keeps coming back with 0 for everyone. Any help appreciate!


Overview

For this assignment, write a program that will process a file of data about winners of the World Series between 1903 and 2015. It will allow the user of the program to name a baseball team and learn how many times that the team has won the World Series.



Processing

The main function for this program is pretty basic. It should start by creating an array that can hold a maximum of 120 string elements. Each element in the array will be the name of a team that won the world series.

Call the buildArray function that is described below to fill the array with the names of the teams.

Prompt the user for the name of a baseball team and save the value in a string variable.

Call the numWins function that is described below to find the number of times the selected team has won the World Series.

Finally, display the number of times that the selected team has won the World Series.


Input File

The file with the input data can be downloaded and saved on your computer. It is called WorldSeriesWinners.txt and can be found here:

http://faculty.cs.niu.edu/~byrnes/csci240/pgms/WorldSeriesWinners.txt

The file contains the names of the teams that have won the World Series in the order that they were won from 1903 through 2015. The World Series was not played in the years 1904 and 1994. Therefore, those two years have the string "Not Played" rather than a team name.




Reading a string

In the previous assignments, whenever an input operation was performed, the input operator (>>) was used with either cin or an input file stream variable. This worked because the information that was being inputted was either separated by whitespace (a space or a newline character) or was being read one character at a time.

For this assignment, the input operator CANNOT be used because the team names will have at least one space separating the parts of the name. For example:

Chicago Cubs
New York Yankees

Rather than trying to read the team names in parts, it should be read as a "whole" string. To do this, the getline function must be used. The format for getline is:

getline( name_of_the_input_stream, name_of_the_string_to_hold_data );
So, if the information should come from standard input (the keyboard):

string str;

getline( cin, str );
or from an input file:

string str;

getline( infile, str );


The Functions

For this program, two functions are required:

int buildArray( string team_array[] )

This function will place the values from the input file into the passed in array of strings.

It takes one argument: an array of strings that will be filled with the names of the teams that have won the World Series. It returns an integer: the number of teams that were placed into the array.

This function should use a standard read loop pattern to read the data from the file and place it into the array. Read the first team name from the file. In a loop that executes while there is data in the file, put the team name that was read into the array, update the array subscript, and get another name from the file.

Once all of the data has been read from the file, return the number of teams that were placed into the array.

int numWins( string team_array[], int numTeams, string search_team )

This function will search the array of World Series winning teams to determine the number of times that a specific team has won the World Series.

It takes three arguments: an array of strings that will be searched, an integer that represents the number of teams that are in the array (ie. the number of teams to be searched), and a string that represents the specific team to search for in the array. It returns an integer: the number of times that the specified team was found in the array.

In a loop that executes exactly one time for the number of teams that are in the array (ie. numTeams number of times), check to see if the specific team to search for in the array (ie. search_team) is found in the array of strings that will be searched (ie. team_array). If the specific team is found, increment a counter by 1.

Once the loop is finished executing, return the counter.




Symbolic Constant

This program requires the use of 1 symbolic constant. The constant should represent the maximum number of teams that can be placed into the array. The value should be 120.


Output

The output will depend on the teams that the user specifies.


Run 1


Enter a team: Chicago Cubs


The Chicago Cubs have won the World Series 2 time(s).




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

using namespace std;

//Function that builds the array
int build_array( string team_array[] )
{
    int cnt = -1;
    
    string teamName;
    
    //Opening file in reading mode
    fstream fin("WorldSeriesWinners.txt", ios::in);
    
    //Loop till data exists
    while(fin.good())
    {
        //Fetching a team name
        getline(fin, teamName);
        
        //Excluding Not Played data
        if(strcmp(teamName.c_str(), "Not Played") != 0)
        {
            //Incrementing count
            cnt++;
            //Storing in array
            team_array[cnt] = teamName;
        }
    }
    
    //Closing file
    fin.close();
    
    return cnt;
}


//Function that find number of wins
int numWins( string team_array[], int numTeams, string search_team )
{
    int i, cnt = 0;
    
    //Loop over teams
    for(i=0; i<numTeams; i++)
    {
        //Comparing team names
        if(strcmp((team_array[i]).c_str(), search_team.c_str()) == 0)
            cnt++;
    }
    
    //Returning count
    return cnt;
}

//Main function
int main()
{
    const int maxTeams = 120;
    
    string teamArray[maxTeams];
    
    string teamName;
    
    int wins, numTeamsRead;
    
    //Building array
    numTeamsRead = build_array(teamArray);
    
    //Reading team name
    cout << "\n\n Enter a team: ";
    getline(cin, teamName);
    
    //Finding number of wins
    wins = numWins(teamArray, numTeamsRead, teamName);
    
    //Printing result
    cout << "\n\n The " << teamName << " have won the World Series " << wins << " time(s) \n";
    
    return 0;
}
Last edited on
I had to add the line
#include <cstring>
in the header, in order to use strcmp() - otherwise your program worked fine!

Maybe you haven't stored the data file correctly on your computer:
(a) check it is in your run directory;
(b) check that it is called WorldSeriesWinners.txt (including correct case);
(c) check that it has the requisite content;
(d) why don't you print out the value of numTeamsRead after you build the array to check that the file has been read OK; also check that filestream fin opened OK.
This is what I have now... Is my file directory right? Idk.. I added file to prog7 on c++ complier too. Still running with 0 for everyone.



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

using namespace std;

//Function that builds the array
int build_array( string team_array[] )
{
    int cnt = -1;
    
    string teamName;
    
    //Opening file in reading mode
    fstream fin("/Users/Tanner/Desktop/prog7/prog7/WorldSeriesWinners.txt", ios::in);
    
    //Loop till data exists
    while(fin.good())
    {
        //Fetching a team name
        getline(fin, teamName);
        
        //Excluding Not Played data
        if(strcmp(teamName.c_str(), "Not Played") != 0)
        {
            //Incrementing count
            cnt++;
            //Storing in array
            team_array[cnt] = teamName;
        }
    }
    
Your code looks reasonable at first glance, although there is room for improvement.

Referring to your original post:

Line 17: I would strongly suggest that you check that the file was opened.
17
18
19
20
  if (fin.fail())
  {  cout << "Failed to open input file" << endl;
      exit (EXIT_FAILURE);
   }


Line 18: If you fail to open the file, you will return -1 as the number of teams read. This will cause numWins to return 0. I suggest you add a cout at line 32 for debugging purposes:
 
  cout << "Number of teams read = " << cnt << endl;


Line 24,49: This is C++. Don't use strcmp to compare to std::string variables. std::string supports the == and != operators.
 
  if (teamName !=  "Not Played")


Line 18: Do not loop on fin.good(). This does not work the way you expect. The eof bit is set true only after you make a read attempt on the file. This means after you read the last record of the file, eof is still false. Your attempt to read past the last record (line 22) sets eof, but you're not checking it there. You proceed as if you had read a good record. This will result in reading an extra (bad) record. The correct way to deal with this is to put the >> or getline operation as the condition in the while statement.
1
2
3
  while (getline(fin,teamName)) 
  {  //  Good operation
  }


Even if the array is loaded correctly, what the user types must match the array exactly with regard to capitalization and spacing.
Last edited on
Okay I did that and it shows up -1. I don't get how I'm not opening file right.
Is your file (a) on your computer, (b) in the directory from which you are running the program and (c) not corrupted by the download via your browser? Check that you can simply view it with Windows notepad (assuming that you are running on Windows).

Once I put the appropriate header in your program ran perfectly fine for me.

Which compiler are you using BTW?
I'm looking awfully hard at that line
fstream fin("/Users/Tanner/Desktop/prog7/prog7/WorldSeriesWinners.txt", ios::in);
Are you absolutely certain that you have two levels of directory called prog7? - highly unlikely. The slashes in a path would you usually go the other way in Windows as well.

I wouldn't run anything off the desktop like this. Put just all your working in the same normal directory. Then you won't have to bother with the whole path: just have
fstream fin("WorldSeriesWinners.txt", ios::in);
Check that you can open and view that data file with the normal Windows notepad. Run your executable program from the command line, not through some coding environment.

I don't think this is a C++ problem.

I use Xcode and I have a MAC... Still nothing.
Do you run to program from your IDE or do you start it directly?
I don't know about Xcode, but if I run a program from my IDE (codeblocks) the file has to be in the same directory as the project file (which is <the project directory>).
If I run a program directly, the file has to be in the same directory as the executable (which is <the project directory>/bin).
Topic archived. No new replies allowed.