Segmentation fault in the very last for loop

Oct 21, 2018 at 8:22pm
closed account (EAp4z8AR)
I have a file that I am supposed to retrieve names from and make a list of it. There is something wrong with the candidates vector because when I try to call it for the output I get a segmentation fault.

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
  int main()
{
    string name;
    vector<string> candidates;
    vector<int> votes;
    vector<double> percents;
    
    //setup for random number generation
    srand(time(0));
    
    //rand % n gives you random numbers between 0 and n-1 (inclusive)
    //generating random number of votes
    for (int i=0; i<=16; i++)
    {
        int x = rand()%9000;
        while (x<1500)
        {
            x = rand()%9000;
            if (x>=1500)
            {
                votes.push_back(x);
            }
        }
        votes.push_back(x);
        i;
        //cout << votes[i] << "\n";
    }
    
    //vote percent
    double sum = accumulate( votes.begin(), votes.end(), 0.0);
    for (int i=0; i<=16; i++)
    {
        int x = (votes[i]/sum)*100;
        percents.push_back(x);
        //cout << percents[i] << "\n";
    }
    
    //creating inputfile object
    ifstream inputfile;
    
    //linking the object to a text file
    inputfile.open("candidates.txt");
    
    //determining if the file was properly opened
    if (!inputfile)
    {
        cout << "not opened" << endl;
        return -1;
    }
    
    //reading from the file into a variable
    //determining if there is anything left to read from the file
    while (inputfile >> name)
    {
        candidates.push_back(name);
    }
    
    
    cout << "Candidate" << setw(10) << right << "Votes" << setw(13) << right << "Percents";
    for (int i=0; i<=16; i++)
    {
        cout << candidates[i] << setw(10) << right << votes[i] << setw(13) << right << percents[i] << "%\n";
    }
}
Oct 21, 2018 at 8:25pm
closed account (EAp4z8AR)
But if I have this for the name input it prints the names.


1
2
3
4
while (inputfile >> name)
    {
        cout << name << endl;
    }
Oct 21, 2018 at 8:29pm
How many candidates are there in the file? Line 60 implies (at least) 17.

I suggest that you read the candidates' names first, and work with candidates.size(), not the magic number 16. (In a British election that is a huge number!).

Remove lines 19-22 or you will have some vote-rigging!
Oct 21, 2018 at 8:33pm
closed account (EAp4z8AR)
There are 16
Oct 21, 2018 at 8:37pm
closed account (EAp4z8AR)
Ahhh yes moving the candidate stuff to the beginning helped thank you so much!
Oct 21, 2018 at 8:37pm
16 candidates ... but on line 60 you try to print the 0, 1, 2 ... 16 members. i.e 17 of them

The last element of candidates is candidates[n-1], where, in your case, n=16.

I repeat, use
< candidates.size()
rather than
<= 16
This also means you should read the candidates at the start, so that you have that size.
Topic archived. No new replies allowed.