c++ world series winners

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

int main()
{
cout << "\nWorld Series Champios";
cout << "--------------------------------------";

const int YEAR=28;
string worldSeries[YEAR];
string name,
index;
ifstream inputFile;
int count=0,
win=0;

inputFile.open("teams.txt");

if (inputFile)
{
while (getline(inputFile,index))
{
cout << index << endl;
}
}
inputFile.close();
cout << "\nPlease enter the name of the winner "
<< "team you want to search: ";
getline(cin,name);
inputFile.open("worldSeries.txt");
if (inputFile)
{
while (getline(inputFile,worldSeries[count]))
{
if (worldSeries[count]==name)
{
win++;
}
count++;
}
}
inputFile.close();
cout << "\nReport";
cout << "\nFrom 1903 to 2012, the team "
<< name << " has won " << win
<< " times. " << endl;
return 0;
}

Can somebody help me to view my program?
When I run it, it shows segmentation fault(core dump)
while (getline(inputFile,worldSeries[count]))
Maybe you try to read more than 28 lines into your array and you access it with an illegal index.
So better check your count before you use it -like so.
while (count < 28 && getline(inputFile,worldSeries[count]))
If this doesn't help you need to post your input files.

BTW. Do you have to use an array? Would be easier to use a vector.
Last edited on
Line 46 implies worldSeries.txt has data for 109 years.
Your array only holds 28 years.
Any attempt to store more than 28 entries in your array is going to cause an out of bounds reference.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Topic archived. No new replies allowed.