segmentation fault (core dumped)

I keep getting 'segmentation fault (core dumped)' what is causing it? How do I fix it?

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

using namespace std;

struct item {
    int price;
    string type;
    int action;
};

item arra [100];

int main (){
    string filename;
    cin >> filename;
    ifstream file;
    string line;
    int fileread;
    fileread=0;
    item temp;
    string conprice;
    int inprice;
    string intype;
    string inaction;
    inprice = 0;
    int action;
    int searchCounter;
    int shiftCounter;
    int i;
    i =0;
    string n;
    n = ",";
    int m;
    int pos;
    shiftCounter = 0;
    searchCounter = 0;
    cout << "Items sold." << endl;
    file.open(filename.c_str(),ios::in);
    while (getline(file,line)){
        pos = line.find(n);
        intype = line.substr(0,pos);
        m = line.substr(pos+1,-1).find(n);
        inaction = line.substr(pos,m);
        conprice = line.substr(m+1,-1);
        stringstream convert(conprice);
        convert >> inprice;
        temp.price = inprice;
        temp.type = intype;
        if (inaction == " for sale"){
            temp.action = 0;
            action = 0;
        }
        if (inaction == " wanted"){
            temp.action = 1;
            action = 1;
        }
        fileread++;
        for (int v;v<100;v++){
            searchCounter++;
            if (intype == arra [v].type){
                if (action == 1){
                    if (action == 0){
                        if (inprice <= arra [v].price){
                            //match
                        }
                        else {
                            arra [i] = temp;
                            i++;
                            shiftCounter++;
                        }
                    }
                    else {
                        arra [i] = temp;
                        i++;
                        shiftCounter++;
                    }
                }
                else if (action == 0){
                    if (action == 1){
                        if (inprice >= arra [v].price){
                            //match
                        }
                        else {
                            arra [i] = temp;
                            i++;
                            shiftCounter++;
                        }
                    }
                    else {
                        arra [i] = temp;
                        i++;
                        shiftCounter++;
                    }
                }
            else {
              arra [i] = temp;
              i++;
              shiftCounter++;
            }
            }
        }
    }
    file.close();
    cout << "Items remaining in the message board after reading all lines in the file." << endl;
    for (int w;w<=i;w++){
        if (arra [w].action == 0){
            cout << arra [w].type << ", " << "for sale" << ", " << arra [w].price << endl;
        }
        else if (arra [w].action == 1){
            cout << arra [w].type << ", " << "wanted" << ", " << arra [w].price << endl;
        }
    }
    cout << "Loop iterations." << endl;
    cout << "file read:" << fileread << endl;
    cout << "search array:" << searchCounter << endl;
    cout << "shift array:" << shiftCounter << endl;
    return 0;
}
Line 61: You never assign any value to v.
Line 108: Same problem.

By the way, avoid doing this:
1
2
SomeType someVariable;
someVariable = someValue;
If you're going to initialize someVariable anyway, do it at declaration. That way someone scanning through the code doesn't need to check ahead to see if you actually initialized it:
 
SomeType someVariable = someValue;
Last edited on
Thank you so much
Topic archived. No new replies allowed.