Loading array with ints from file

This function is causing my program to crash. No compile errors though.

I'm trying to load integers from a file into an array, then count the times those integers show up in the array:

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
void countNumbers( int &count0, int &count1, int &count2, int &count3,
                    int &count4, int &count5, int &count6, int &count7,
                     int &count8, int &count9, ifstream &fin, int size )
     {
      int test, arr[ size ], current, count = 0;

            fin.ignore( 100, '.' );
            fin >> current;
            while( !fin.eof() )
                {
                  arr[ count ] = current;
                  fin >> current;
                  count++;

                }

            while( count < size )
              {

               arr[ count ] = test;
                   if( test == 0 )
                       {
                       count0++;
                       }
                   if( test == 1 )
                       {
                       count1++;
                       }
                   if( test == 2 )
                       {
                       count2++;
                       }
                   if( test == 3 )
                       {
                       count3++;
                       }
                   if( test == 4 )
                       {
                       count4++;
                       }   
                   if( test == 5 )
                       {
                       count5++;
                       }           
                   if( test == 6 )
                       {
                       count6++;
                       }         
                   if( test == 7 )
                       {
                       count7++;
                       }
                   if( test == 8 )
                       {
                       count8++;
                       }
                   if( test == 9 )
                       {
                       count9++;
                       }
               count++;
              }
     }


Any tips? Anyone see whats causing the crash?
Figured out one thing. I should set two seperate counters for the loop. So I set a counter1 = 0 for the first loop and a counter2 = 0 for the second loop. Still crashes the program
Try this:
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
void countNumbers( int &count0, int &count1, int &count2, int &count3,
                    int &count4, int &count5, int &count6, int &count7,
                     int &count8, int &count9, ifstream &fin, int size ){
	int i = 0, test, arr[size], current;

	fin.ignore( 100, '.' );
	while(!fin.eof()){
		fin >> current;
		arr[i] = current;
		i++;
	}
	i = 0;
	while(i < size){
		arr[i] = test;
		switch(test){
			case 0:
				count0++;
				break;
			case 1:
				count1++;
				break;
			case 2:
				count2++;
				break;
			case 3:
				count3++;
				break;
			case 4:
				count4++;
				break;
			case 5:
				count5++;
				break;
			case 6:
				count6++;
				break;
			case 7:
				count7++;
				break;
			case 8:
				count8++;
				break;
			case 9:
				count9++;
				break;
		}
		i++;
	}
}

I swapped
1
2
arr[i] = current;
fin >> current;

to
1
2
fin >> current;
arr[i] = current;


EDIT: Changed while(int i .... ) to while(i ....).
Last edited on
Nice thanks, didn't think to use switch. It's still crashing though. Heading to bed, sleep should help.
Topic archived. No new replies allowed.