Segmentation Fault error help when using arrays.

I've been trying to figure out how to solve the segmentation fault. I put up a partial of the code and the text file of the code I am reading. Trying to put numbers in an array. I'm trying to make it stop reading at -1. thanks guys!!

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
7111
3038
7381
4832
6959
4071
7640
5561
8591
6580
7356
662
9663
2929
4243
7267
7103
7439
2302
7373
8450
2107
2290
2465
6161
-1


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
  #include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <cmath>
using namespace std;


int main()

{  ifstream fin;
   ofstream fout;
   int i=0;
   int inputs[i];
   int tmp;
   int const SIZE=500;

   int avg;
   double stdev;
   double numerator;

   fin.open("lab6_input.txt");


   fin>>tmp;



   while(i<SIZE && tmp!=-1)
   {
       inputs[i]=tmp;
       //tmp=inputs[i];
      cout<<inputs[i]<<endl;
i++;
fin>>tmp;
   }
1
2
int i = 0;
int inputs[i];

can't have an array of size zero

maybe you wanted.
1
2
int const SIZE=500;
int inputs[SIZE];
Last edited on
14
15
   int i=0;
   int inputs[i];


Hi Hashamali96,

With that code, the array inputs has size 0, probably hence the seg fault.

Better to declare input to be of size SIZE.

Also, be careful to initialise your variables.

HTH
Thanks guys!

I, however, need to reread the whole chapter about arrays!
Topic archived. No new replies allowed.