Segmentation fault (core dumped)

cant figure out whats wrong

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
  1 #include<iostream>
  2 #include<fstream>
  3 #include<cmath>
  4 
  5 using namespace std;
  6 
  7 long long array[10]={0},tmp[]={0},cnt=0;
  8 int num = 0;
  9 int j = 0; 
 10 long long temp = 0;
 11 int i;
 12 /* Functions that determine if numbers are  prime, odd, even, and
 13    gives the top 10 largest values.
 14 */ 
 15 void addToLargest(){
 16   for(i=0;i<cnt;i++){
 17   for(j=i;j<cnt;j++){
 18     if(tmp[i] < tmp[j]){
 19     temp = tmp[i];
 20     tmp[i]=tmp[j];
 21     tmp[j]=temp;
 22   }
 23   }
 24   }
 25 
 26   cout<<"Ten largest numbers: "<<endl;
 27   for (i = 0; i < 10; i++){
 28    cout<<tmp[i]<<tmp[i++]<<endl;
 29   }
 30 }
 31 //Prime
 36   }
 37   for(num=3; num <= mysqrt; num += 2) {
 38     if (pn%num==0) return(false);
 39     }
 40     return(true);
 41 
 42 }
 43 bool isOdd (int on) {
 44   return(on % 2 != 0);
 45 }
 46 bool isEven (int en) {
 47   return(en % 2 == 0);
 48   }
 51   that include the prime numbers, evens , and odds.
 52 */
 53 int main() {
 54 //Opens file input.txt  
 55   ifstream inFile;
 56   inFile.open("input.txt");
 57 //Initialize Variables  
 58   int totalsize = 0;
 59   int count = -1;
 60   double en = 0;
 61   int on = -1;
 62   int pn = -1;
 63   int num = 0;
 64 //Creates and opens primes.txt, evens.txt, and odds.txt
 65   ofstream primes;
 66   primes.open("primes.txt");
 67   ofstream evens;
 68   evens.open("evens.txt");
 69   ofstream odds;
 70   odds.open("odds.txt");
 71 
 72   while (totalsize !=-1) {
 73     inFile>>totalsize;
 74     count ++;
 75     tmp[cnt++]=totalsize;
 76     if (isEven (totalsize)) {
 77       en++;
 78       evens<<totalsize<<endl;
 79     }
 80     if (isOdd (totalsize)){
 81       on++;
 82       odds<<totalsize<<endl;
 83     }
 84     if (isPrime(totalsize)) {
 85       pn++;
 86       primes<<totalsize<<endl;
 87     }
 88 
 89   }
 90   addToLargest();
 91 
 92   inFile.close();
 93   inFile.close();
 94   cout<<"Numbers read in: "<<count<<endl;
 95   cout<<"Even Numbers: "<<en<<endl;
 96   cout<<"Odds Numbers: "<<on<<endl;
 97   cout<<"Prime Numbers: "<<pn<<endl;
 98 
 99   }
When is totalsize every set to -1? In other words, when do you stop attempting to read from inFile and when does count, and more importantly cnt (that indexes into tmp) stop being incremented?

Speaking of tmp, do you see that it's just large enough for 1 element?
Last edited on
know, i didnt know tmp could hold only one element and -1 is the sentinel value. This is my first class using c++ and it is rather confusing to me but im gradually learning. What do you suggest i do to help solve my problem
Topic archived. No new replies allowed.