printing string array getline'd from a textfile in alph order

Hi,

For a class assignment, I have to open a text file, grab the information in the file, put it into a string array, and print it out a couple of ways.

I can print it out with a simple space between words the way the list is written in the text file, but I can't figure out how to also print it out in alphabetic order.

Can anyone help me with 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
  
#include <iostream>
#include <fstream> 
#include <string>
//#include <iomanip>
using namespace std;

const int MAX = 170;

int load(string fileName, string word[])
{
    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] << " " ;
        
    }

    return count;
}

int main()
{
    string word[MAX];
    string prodNames[MAX];
    string fileName;
    string x;
    
    cin >> fileName;
  
    fileName = load(fileName, word); 
}
Last edited on
You need to sort the array. Then print it out.
Topic archived. No new replies allowed.