My array sorting isn't working

Hi,
Arrays are a really weak area of mine. I have a program that opens text files(the files contain a simple list of words all lowercase in no particular order).

It opens the file, stores the list into a string array, and can print out the list and also print how many there were total. My problem is the sorting the list into alphabetical order. My sort is throwing confusing errors about no declaration and other things.

Since you cant open the text files, I've left comments where the focus areas are, and you can assume that it is part of a program that otherwise works if I take out the indicated sort code.

Thank you.

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
#include <iostream>
#include <fstream> 
#include <algorithm>
#include <iterator>
#include <string>

using namespace std;

const int MAX = 170;

int load(string fileName, string word[]) // Jeffrey Jordan
{
    int count = 0;
    ifstream file;
    
    file.open(fileName.c_str());
    string word1;
    getline(file, word1);
    
    while(file) 
    {
        
        word[count] = word1;
        count ++ ;
        getline(file, word1);
        
    }
    
    cout << "There were " << count << " words read." << endl;
  
    for(int i = 0; i<count; i++)
    { 
        cout << word[i] << " " ; // This prints the file as it is read.
    
        sort(begin(word), end(word)); // problem here with printing alphbtcly
            for(auto& s: word) // Do I need to take this section out of the
                cout << s << ' '; // same loop as the regular print loop?
    }
        
    return count;
}

int main()
{
    string word[MAX];
    string prodNames[MAX];
    string fileName;
    string x;
    
    cin >> fileName;
  
    fileName = load(fileName, word); 
}
Last edited on
C-style arrays decay into pointers when they are passed to functions.
The traditional way to deal with this problem is to pass the the array dimension as one of the argument of the function.
Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

void printArray(std::string * myarr, unsigned size);


int main()
{
    std::string an_arr[] { "Sheet", "Duvet", "Pillow" };
    
    unsigned arr_size = sizeof(an_arr) / sizeof(an_arr[0]);
    
    printArray(an_arr, arr_size);
}


void printArray(std::string * myarr, unsigned size)
{
    for(unsigned i {}; i < size; ++i) {
        std::cout << myarr[i] << ' ';
    }
    std::cout << '\n';
}


To use std::sort() on your array, now become a pointer, you don’t need iterators (it’s already a pointer!):
E.g.: std::sort(myarr, myarr + size);

The problem is there are several errors in your code. For example, std::sort() shouldn’t be inside a for loop, i.e. this block of code is conceptually wrong:
1
2
3
4
5
6
7
8
    for(int i = 0; i<count; i++)
    { 
        cout << word[i] << " " ; // This prints the file as it is read.
    
        sort(begin(word), end(word)); // problem here with printing alphbtcly
            for(auto& s: word) // Do I need to take this section out of the
                cout << s << ' '; // same loop as the regular print loop?
    }


(You want your array to be sorted just once, don’t you?)

And you can’t use range-for with pointers, too, so the last two rows in the above code won’t compile.

Please, let us know if that’s an assignment: I can’t give complete answers to assignments.
Topic archived. No new replies allowed.