简体   繁体   中英

SIGSEGV, segmentation fault error when declaring a variable

I'm receiving this error I have it narrowed down to the string teamsTemp; line. What I understand from reading other posts is this normally results from not having a variable declared. In this case it is erroring out when I declare a variable. Even with the rest of the bubble sort commented out it throws an error on that variable.

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


int main()
{
    ifstream WinnersIn;     
    ifstream TeamsIn;           
    ifstream LeagueIn;        
    string inputWinner;
    string inputTeam;
    string inputLeague;
    int wins[30] = {0};
    string teams[30];
    string winner[114];
    string league[30];
    int t = 0;
    int w = 0;
    int z = 0;
    int year = 1903;
    int j;

    TeamsIn.open("Teams.txt");
    LeagueIn.open("Leagues.txt");
    WinnersIn.open("WorldSeriesWinners.txt");

    //Saving teams into an array
    while (getline(TeamsIn, inputTeam))
    {
        teams[t] = inputTeam;
        t++;
    }

    //Saving Winners into an array
        while (getline(WinnersIn, inputWinner))
    {
        winner[w] = inputWinner;
        w++;
    }

    //Saving division Winner names into an array
        while (getline(LeagueIn, inputLeague))
    {
        league[z] = inputLeague;
        z++;
    }

    //Nested for loop to match winning team and their league then output
    for (int i=0; i < 115; i++)
    {
        for (z=0; z < 30; z++)
        {
            if (winner[i] == teams[z])
            {
                cout << year << " " << winner[i] << " " << league[z] << endl;
                year++;
            }
        }
    }

    //Saving number of wins for each team into an array
    for (int i = 0; i < 115; i++)
    {
        for (z=0; z < 30; z++)
        {
            if (winner[i] == teams[z])
            {
                wins[z] = wins[z] + 1;
            }
        }
    }


    //Sorting by number of wins
    for (int i = 0; i < 29; i++)
    {
        for (int j = 0; j < 30; j++)
        {
            if (wins[i] < wins[j]) 
            {
                int winsTemp;
                string teamsTemp;

                /*winsTemp = wins[i];
                wins[i] = wins[j];
                wins[j] = winsTemp;

                teamsTemp = teams[i];
                teams[i] = teams[j];
                teams[j] = teamsTemp;*/
            }
        }
    }

    TeamsIn.close();
    LeagueIn.close();
    WinnersIn.close();

    return 0;
}

leagues.txt American League National League National League American League American League National League American League National League National League American League National League American League American League National League American League National League National League American League National League National League American League No Winner American League American League National League National League National League National League American League American League

teams.txt Anaheim Angels Arizona Diamondbacks Atlanta Braves Baltimore Orioles Boston Americans Boston Braves Boston Red Sox Brooklyn Dodgers Chicago Cubs Chicago White Sox Cincinnati Reds Cleveland Indians Detroit Tigers Florida Marlins Kansas City Royals Los Angeles Dodgers Milwaukee Braves Minnesota Twins New York Giants New York Mets New York Yankees No Winner Oakland Athletics Philadelphia Athletics Philadelphia Phillies Pittsburgh Pirates San Francisco Giants St. Louis Cardinals Toronto Blue Jays Washington Senators

World series winners is just a list of teams that have won the world series every year.

winner is an array of size 114 but i can go up to 114 which accesses element 115, which is off the end of your array.

        if (winner[i] == teams[z])

On top of that you have at least a couple places where you're reading into an array but it's not clear that you're limiting your input to the size of the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM