HELP! ARRAYS COUNTING OUTPUT

I use C++ on Mac xCode.
I keep getting the right output meaning my file is opening but it won't count up the right data on the files. Ill post what outlook is suppose to look like also. Mine just keeps saying every team I enter wins 0 world series.

Thanks!

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
160
161
162
163
164
165
166
167
168
169
170
171
172
173

#include <iostream>

#include <fstream>

#include <string>

#include <cstring>


using namespace std;

//  builds the array

int build_array(string team_array[])

{
    
    int count = -1;
    
    string teamName;
    
    // Opens file
    
    fstream fin("/Users/Tanner/Desktop/7/program777/program777/WorldSeriesWinners.txt", ios::in);
    
    // Loop till data exists
    
    while (fin.good())
        
    {
        
        // get team name
        
        getline(fin, teamName);
        
        // Not Played data
        
        if (strcmp(teamName.c_str(), "Not Played") != 0)
            
        {
            
            // Inc count
            
            count++;
            
            // Store in array
            
            team_array[count] = teamName;
            
        }
        
    }
    
    // Close file
    
    fin.close();
    
    return count;
    
}

//  fidning number of wins

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

{
    
    int index, count = 0;
    
    // Loop over teams
    
    for (index = 0; index<numTeams; index++)
        
    {
        
        // comparingthe  team names
        
        if (strcmp((team_array[index]).c_str(), search_team.c_str()) == 0)
            
            count++;
        
    }
    

    return count;
    
}

void displayYears(string team_array[], int numTeams, string search_team)

{
    
    int index, count = 0,line=0;
    
    // Loop over teams
    
    for (index = 0; index<numTeams; index++)
        
    {
        
        // Comparing team names
        
        if (strcmp((team_array[index]).c_str(), search_team.c_str()) == 0)
            
        {
            
            line++;
            
            cout << 1904 + index << " ";
            
            count++;
            
            if (line == 7)
                
            {
                
                cout << "\n";
                
                line = 0;
                
            }
            
        }
        
    }
    
}

// Main func

int main()

{
    
    // constant
    
    
    
    const int maxTeams = 120;
    
    //variables
    
    
    
    string teamArray[maxTeams];
    
    
    string teamName;
    
    
    int wins, numTeamsRead;
    
    // Call build array
    
    numTeamsRead = build_array(teamArray);
    
    // Reading name
    
    cout << "Enter a team: ";
    
    getline(cin, teamName);
    
    //  number of wins
    
    wins = numWins(teamArray, numTeamsRead, teamName);
    
    //  result
    
    cout << "\nThe " << teamName << " have won the World Series " << wins << " time(s) \n";

}


Output is suppose to be:

Enter a team: Chicago Cubs

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


Input file is... http://faculty.cs.niu.edu/~byrnes/csci240/pgms/WorldSeriesWinners.txt
The code works as is (at least for the New York Giants). To make it work: it has to be able to find and open the file and the team name you enter has to have the exact same capitals and spacing.
For the later you may want to have a look at strcmpi.

To check why it is not working on your system I would suggest adding a lot of cout's with intermediate results.
Topic archived. No new replies allowed.