sorting input file alphbetically

See input file below. I am trying to create a void function that would take two arrays (response and category) from this input file, sort them alphabetically and then print them to the screen. void function is to be called in the main without using vectors I don't want the complete answer but I have been working all day and am completely lost. Can any of you point me in the right direction? This would fill in option number 3 in the menu.

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream>
#include<cstdlib>
#include<string>
#include<fstream>

using namespace std;

void readFromfile(string filename, string response[], string category[], int& maxResponses);
void magicEight(string filename, string response[], string category[], int& maxResponses);



int main (){

    bool done=false;
    int menu;
    const int MAXSIZE = 100;
    string  response[MAXSIZE], category[MAXSIZE], answer, categories;
    int size=0;
    while(!done){
        cout<<"Select one of the options below\n";
        cout<<"1. Read responses from file\n";
        cout<<"2. Play Magic 8 Ball.\n";
        cout<<"3. Print out responses and categories alphabetically\n";
        cout<<"4. Exit\n";

        cin>>menu;
        switch(menu){
            case 1 :
                cout<<"Read from file\n";
                readFromfile("answers.txt", response, category, size );
                break;
            case 2:
                cout<<"Play Magic 8 Ball\n";
                magicEight("answers.txt", response, category, size);
                break;
            case 3:
                cout<<"Print out responses alphabetically\n";
                writeToFile();
                break;
            case 4:
                cout<<"You will now exit menu.\n";
                done=true;
                break;
            default:
                cout<<"Invalid option.\n";

        }
    }

    return 0;
}
void readFromfile(string filename, string response[], string category[], int& maxResponses) {
    string answer, cats;
    int i = 0, j = 0, k = 0;
    ifstream infile(filename);
    while (getline(infile, answer)) {
        if (answer.back() == '\r') {
            answer.pop_back();
        }
        getline(infile, cats);
        response[maxResponses]=answer;
        category[maxResponses]=cats;
        cout<<response[maxResponses]<<" "<<category[maxResponses]<<endl;


It is certain
positive
It is decidedly so
positive
Without a doubt
positive
Yes definitely
positive
You may rely on it
positive
As I see it, yes
positive
Most likely
positive
Outlook good
positive
Yes
positive
Signs point to yes
positive
Reply hazy try again
vague
Ask again later
vague
Better not tell you now
vague
Cannot predict now
vague
Concentrate and ask again
vague
Don't count on it
negative
My reply is no
negative
My sources say no
negative
Outlook not so good
negative
Very doubtful
negative 
Last edited on
I would suggest you look into std::multiset instead of an std::string array, because std::multiset<string> will automatically insert them in alphabetic/lexicographical order.

See here for more information on how to use it:
http://www.cplusplus.com/reference/set/multiset/
https://en.cppreference.com/w/cpp/container/multiset


In order to print both the responses and the categories, you can do this easily:
1
2
3
4
5
6
std::cout << "Categories: \n";
std::copy(std::cbegin(category), std::cend(category) 
          std::ostream_iterator<std::string>(std::cout, "\n"));
std::cout << "Responses: \n";
std::copy(std::cbegin(responses), std::cend(responses) 
          std::ostream_iterator<std::string>(std::cout, "\n"));


Make sure you include <algorithm> and <iterator>, as well as <set>
Last edited on
thanks toaster but I can't use that yet. have to show that I can do it without a multiset
If you can't use std::multimap, then I'm also going to assume you cant use any standard library algorithms like std::sort().

In that case, you are going to have to write a function that sorts your array into alphabetical order, a selection sort algorithm, and then you can print out the array normally using a for loop. std::strings can be compared as if they were integers using its operator<. Here is a link that explains how to make a selectionSort function. You can just change it around for std::strings rather than the integer example they give:
https://www.geeksforgeeks.org/selection-sort/

Once you have the selectionSort function, call the function with both your responses[] and your categories[] and sort both of them.

The key to understanding how to do it is to understand that strings can be compared to each other.

For example, "B" > "A" == true;

Last edited on
thanks toaster I'll work on it and get bAck
Topic archived. No new replies allowed.