Why is this code throwing me a segmentation fault?

closed account (y0q21hU5)
I'm working on factoring out of a nth root problem and I have started my code but am having trouble as it keeps segmentation faulting when I test it. Any help would be great. Thanks!

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
 #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
	int base;
	int root;
	cin >> base;
	vector<int> factors;
	int temp = base;
	int x;
	for(x = 2; x <= temp; x++){
		if(temp % x == 0){
			factors.push_back(x);
			temp = temp/x;
			x = 1;

		}
	}

	cout << factors.size() << endl;
	for (unsigned int i = 0; i < factors.size(); i++){
		cout << factors[i]<<endl;
	}


	vector<int> vars(factors);



	vars.erase(unique(vars.begin(), vars.end()),vars.end());

	vector<int> varCount;


	for(int i = 0; i < vars.size()-1; i++){
		varCount[i] = count(factors.begin(), factors.end(), vars.at(k));


	}



}
where is the k variable defined? vars.at(k));
Also, line 38 should probably be
varCount.push_back(count(factors.begin(), // ...
instead of
varCount[i] = count(factors.begin(), // ...
since varCount initially has size 0 (and operator[] doesn't resize).
Topic archived. No new replies allowed.