Just one error that I cannot deal with...

Hi all,

I'm doing a string selection sort program. However there are some errors in my code that is probably beyond my ability to solve, can someone kindly take a look into it and give some suggestions?

The problem asks me to write a program that can read in large amount of texts from a .txt file and sort in alphabetical orders. This program requires efficiency however right now I am not focusing on that. If anyone can point out some errors in the code that leads the entire program not function well I will be very thankful.

My error is that both string array will not read in the data from a txt file. Can somebody point out the fault ? Thank you!
Header
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
#ifndef SORTINGCOMPETITION_H
#define SORTINGCOMPETITION_H
#include <chrono>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <vector>
#include <sstream>
#include "string.h"
#include "stdlib.h"

using namespace std;

class SortingCompetition
{
private:
    string fileName;
    string* dataSet;
    string* dataSetForSort;
    void resize(string* &data, int size);
    void del();


public:
    SortingCompetition();
    SortingCompetition(const string& inputFileName);

    void setFileName(const string& inputFileName);
    bool readData();
    bool prepareData();
    void sortData();
    void outputData(const string& outputFileName);

};


#endif // SORTINGCOMPETITION_H


Main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "sortingcompetition.h"



int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        cerr<<"parameter number is incorrect"<<endl;
        return 1;
    }

    SortingCompetition* a = new SortingCompetition(argv[1]);
    a->readData();
    a->prepareData();
    a->sortData();
    a->outputData(argv[2]);


    return 0;
}


Cpp
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "sortingcompetition.h"

SortingCompetition::SortingCompetition()
{
    setFileName("");
    dataSet = NULL;
    dataSetForSort = NULL;
}


SortingCompetition::SortingCompetition(const string& inputFileName)
{
    setFileName(inputFileName);
    dataSet = NULL;
    dataSetForSort = NULL;
}


void SortingCompetition::setFileName(const string &inputFileName)
{
    fileName = inputFileName;
}


bool SortingCompetition::readData()
{
    char buffer[50];
    ifstream input(fileName);
    if(!input)
    {
        cerr<<"input file could not be opened"<<endl;
        return false;
    }

    dataSet = new string[10];
    for(int i = 0; !EOF; i++)
    {
        if((i+1)%10 == 0)
        {
            resize(dataSet, sizeof(dataSet)+10);
        }
        input.getline(buffer, 50);
        dataSet[i] = buffer;
    }

    input.close();
    return true;
}


bool SortingCompetition::prepareData()
{
    dataSetForSort = new string[sizeof(dataSet)];
    for(unsigned int i = 0; i < sizeof(dataSet); i++)
    {
        dataSetForSort[i] = dataSet[i];
    }
    return true;
}


void SortingCompetition::sortData()
{
    sort_heap(dataSetForSort->begin(), dataSetForSort->end());
    int startScan, minIndex;
    string minValue;

    for(startScan = 0; startScan < (sizeof(DataSetForSort) - 1); startScan++)
    {
        minIndex = startScan;
        minValue = DataSetForSort[startScan];
        for(int index = startScan + 1; index < sizeof(DataSetForSort); index++)
        {
            if (DataSetForSort[index] < minValue)
            {
                minValue = DataSetForSort[index];
                minIndex = index;
            }
        }
        DataSetForSort[minIndex] = DataSetForSort[startScan];
        DataSetForSort[startScan] = minValue;
    }
}


void SortingCompetition::outputData(const string &outputFileName)
{
    ofstream output(outputFileName);
    if(!output)
    {
        cerr<<"output file could not be opened."<<endl;
        return;
    }

    for(unsigned int i = 0; i < sizeof(dataSetForSort); i++)
    {
        if(dataSetForSort[i] != "")
        {
            output << dataSetForSort[i]<<endl;
        }

    }

    output.close();
    del();
}


void SortingCompetition::resize(string* &data, int size)
{
    string* temp = new string[size];
    for(int i = 0; i < size-10; i++)
    {
        temp[i] = data[i];
    }
    delete []data;
    data = temp;
}

void SortingCompetition::del()
{
    delete []dataSet;
    delete []dataSetForSort;
}


Last edited on
Topic archived. No new replies allowed.