getting segmentation fault (error occured)

Hi I am getting segmentation fault when I try to copy 1 vector to another vector... it occurs only when I try to input stats the second time and then calling the function topFive again..







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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <vector>
#include <array>
#include "PointTwoD.h"
#include "LocationData.h"
#include <math.h>

using namespace std;

vector<PointTwoD> temp;
vector<LocationData> locateVec;
vector<PointTwoD> pointer;
vector<PointTwoD> topFive;

int main()
{
    int printMenu();
    int option;
    void inputStats();
    void computeCivIndex();
    void checkStuff();
    void printTopFive();
    void printTotalTravel();


    do {
        printMenu();
        cout << "Please enter your choice: ";
        cin >> option;
        switch (option)
        {
        case 1:
            inputStats();
            break;
        case 2:
            computeCivIndex();
            break;
        case 3:
            printTopFive();
            break;
        case 4:
            printTotalTravel();
            break;
        case 5:
            cout << "Exiting...." << endl;
            break;
        default:
            cout << "invalid value" << endl;
            break;
        }


    } while(option > 0 && option < 5);

    return 0;
}

void printMenu()
{
    cout << "-----------------------------------------------" << endl;
    cout << "Welcome to Mission Plan program!" << endl << endl;
    cout << "1)         Input statistical data" << endl;
    cout << "2)         Compute civ. index value (for all records)" << endl;
    cout << "3)         Print top 5 exploration destinations" << endl;
    cout << "4)         Print total travel distance" << endl;
    cout << "5)         Quit" << endl << endl;
}


void inputStats()
{
    cout << endl;
    signed short int xordinate, yordinate, numPlanets, numMoons;
    string typeSun = "";
    float partDensity, plasmaDensity;


    cout << "[ Input statistic Data ]" << endl;
    cout << "Please enter x-ordinate: ";
    cin >> xordinate;
    cout << "Please enter y-ordinate: ";
    cin >> yordinate;


    cout << "Please Enter Sun Type: ";
    cin.ignore(100, '\n');
    getline(cin, typeSun);

    cout << "Please Enter no. of earth-like Planets: ";
    cin >> numPlanets;

    cout << "Please Enter no. of earth-like Moons: ";
    cin >> numMoons;

    cout << "Please Enter ave. particulate density (%-tage): ";
    cin >> partDensity;

    cout << "Please Enter ave. plasma density (%-tage): ";
    cin >> plasmaDensity;

    LocationData locationData(typeSun, numPlanets, numMoons, partDensity, plasmaDensity); //initializing locationData Object
    PointTwoD inputPointer(xordinate, yordinate, locationData, 0.0); // initializing PointTwoD object
    pointer.push_back(inputPointer); // inserting into pointerVector
    cout << endl;
    cout << "Record successfully stored. Going back to main menu" << endl << endl;
}


void computeCivIndex()
{
    for (int i = 0; i < pointer.size(); i++){
        string sunTypeTest = pointer[i].getLocationData().getSunType();
        int earthPlanetTest = pointer[i].getLocationData().getNoOfEarthLikePlanets();
        int moonPlanetTest = pointer[i].getLocationData().getNoOfEarthLikeMoons();
        float avePartTest = pointer[i].getLocationData().getAveParticulateDensity();
        float avePlasTest = pointer[i].getLocationData().getAvePlasmaDensity();
        float indexNum = pointer[i].getLocationData().computeCivIndex(sunTypeTest, earthPlanetTest, moonPlanetTest, avePartTest, avePlasTest);
        pointer[i].setCivIndex(indexNum);
    }
    cout << endl;
    cout << "Computation completed! (" << pointer.size() << " records were updated)" << endl << endl;
}


void checkStuff()
{
    cout << pointer.size() << " > pointer size < " << endl;
}


void vectorBubbleSort(vector<PointTwoD> parseInVector)
{
    for (int a = 0; a < parseInVector.size(); a++)
    {
        for (int b = 0; b < parseInVector.size() - 1; b++)
        {
            if(parseInVector[b].getCivIndex() < parseInVector[b + 1].getCivIndex())
            {
                temp[b] = parseInVector[b];
                parseInVector[b] = parseInVector[b + 1];
                parseInVector[b + 1] = temp[b];
            }
        }
    }
}


void printTopFive()
{
    topFive.erase(topFive.begin(), topFive.end());
    topFive = pointer;
    cout << endl;
    cout << "Total no. of records available = " << topFive.size() << endl;
    cout << "Printing top 5 exporation destination ..." << endl;
    int counter = 1;
    int newCounter = 5 - topFive.size();
    vectorBubbleSort(topFive);

    for (int i = 0; i < topFive.size(); i++)
    {
        cout << counter << ")            ";
        cout << "Civ idx : " << topFive[i].getCivIndex() << ", at sector (" << topFive[i].getXvalue() << ", " << topFive[i].getYvalue() << ") " << endl;
        counter++;

    }

    for (int a = 0; a < 5 - topFive.size(); a++)
    {
        cout << counter << ")            ";
        cout << "<no other records available> " << endl;
        counter++;
    }
    cout << endl << "Done!";
    cout << endl << endl;
}

void printTotalTravel()
{
    int defX = 0;
    int defY = 0;
    float dHQtoStarSys = 0;
    float finalDistance = 0;
    float calculateDis = 0;
    for(int i= 0; i < topFive.size(); i++)
    {
        calculateDis = (((topFive[i].getXvalue() - defX) * (topFive[i].getXvalue() - defX)) + ((topFive[i].getYvalue() - defY) * (topFive[i].getYvalue() - defY)));
        dHQtoStarSys += sqrt(calculateDis);
        finalDistance = 2 * dHQtoStarSys;
    }
    float finalMillion = finalDistance * 100;
    cout << endl;
    cout << "Total (approx) travel distance = " << finalMillion << " million km" << endl;
    cout << endl;

}

Last edited on
Why are you using global variables?

Rather than doing topFive.erase(topFive.begin(), topFive.end());, you can clear the container with topFive.clear();. But as you assign another container later on, there's no need to clear it.

void vectorBubbleSort(vector<PointTwoD> parseInVector). As the parameter is passed by value, the work performed is lost.

I don't know what's wrong, I don't want to look further with that structure. I would help if you told us where it stops.

EDIT:
Your bubble sort keeps bubbling even when the sort is complete. The first loop could be improved, or you could stop looping when the swapping ceases. In any event, the standard library has a sort function you could use.

The function prototypes should be outside of main.
Last edited on
Dealing with seg faults is very important as they tend to be hard to detect. There are programs that help doing that, as checkmarx but programming slowly may also help reducing those.
Good luck.
you may should print a mark to analyze where and when the segament fault exactly happened.And then you will find out the cause.
Watch out for this kind of errors as they can spend a lot of your time discovering them. If you need some help you can try using some programs like checkmarx or others but it's recommended to do it on your own.
Good luck!
Topic archived. No new replies allowed.