fill array from file and sort

I am trying to fill an integer array from a file and then sort the array in order and count the number of occurrences of each element. My question is how do i correctly fill the array from the file? What I have is not working correctly, I think it is in line 26 and 27 but im not sure any help is greatly appreciated thanx
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
#include <iostream>
#include <fstream>

void sort(int sample_array[], int size);

int main( )
{
    using namespace std;
    cout << "This program sorts numbers from lowest to highest.\n";

    int sample_array[50], number_used;
    ifstream infile;
    char letter;
    string line;
    infile.open("hw9input.txt");
if (infile.fail())
{
   cout << "input file could not be opened" << endl;
   system("pause");
   exit(1);
}
   int index = 0;
 while (! infile.eof() )
    {
        
       getline (infile,line);
      sample_array[index++] ; 
    }

  cout << endl;
  
    
    sort(sample_array, number_used);

    cout << "In sorted order the numbers are:\n";
    for (int index = 0; index < number_used; index++)
        cout << sample_array[index]<< endl;;
    cout << endl;
system("pause");
    return 0;
}


/////////////////////////////////////////
// sorts array of integers ascendingly //
/////////////////////////////////////////
void sort(int sample_array[], int size)
{
   int out, in, temp;

   for (out = 0; out < size-1; out++)     // for each element index
      for (in = out+1; in < size; in++)   // compare with one element index down
         if (sample_array[out] > sample_array[in])        // if out element index is greater than
         {                                // in element index
            temp = sample_array[in];              // swap them
            sample_array[in] = sample_array[out];
            sample_array[out] = temp;
         } 

} 
Last edited on
i just tried

1
2
3
4
5
6
7
8
9
10
 while (! infile.eof() )
    {
        getline (infile,line); //display the file contents
      cout << line << endl;   
       
    }
for (int index = 0; index < SIZE; index++)
{
getline(infile, numlist[index], ','); // fill the array with the file contents
}


and it still doesn't work
does anyone have any ideas?
I don't think getline() is what you need here. And it's best to avoid eof() as a way of controlling a loop, it can cause more problems than it solves.

1
2
3
4
5
6
7
    int sample_array[50]
    int number_used = 0;
    
    while (infile >> sample_array[number_used] )
    {
        number_used++;
    }

At the end of this loop, all the data has been read into the array, and number_used will contain the count of how many values were read.
Last edited on
thank you so much i've been trying to fill this array for hours now, and of course the answer is super simple, now I can start testing the sort function and adding the counter to count how many times each element occurs in the array. Thank you again
Topic archived. No new replies allowed.