Sorted List Class w/ fstream int&string input

I am trying to build a program that uses the SortedList class, coupled with a text file consisting of integers and strings, where the program will read the integers and the strings, and they will be sorted and stored into the list using the Insert Class within a function called GetList, than a second function called PrintList will use iterators to walk through the list and write the sorted list to a new output file. I have a working model for everything besides the PrintList function, and have searched my c++ books and online examples for something similar to what I need, and am choking on converting it to what I need.

Below is the header file, the implementation file, the main cpp, and the txt file to be read. If you can take a look at my PrintClass function, along with my iterators (specifically the current() iterator) and help me with some ideas or hints to what I should change, it would greatly save me this headache.

SortedList.h
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
  const int MAX_LENGTH = 100;    // Maximum number of components
#include <string>
using namespace std;

struct ItemType
{
    int key;
    string name;
};

class SortedList
{
public:
    void Insert(int key, string name);
    void PrintList();
    ItemType current() const;
    void ResetList();
    void advance();
    bool IsEmpty() const;
    bool IsFull() const;
    int GetLength() const;
    bool HasNext() const;

    SortedList();

private:
    int length;
    int currentPos;
    ItemType data[MAX_LENGTH];
    void BinarySearch(ItemType item, bool& found, int& position);   
};


SortedList.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
#include "sortedList.h"
#include <fstream>

SortedList::SortedList()
{
    length = 0;
}

void SortedList::Insert(int key, string name)
{
    int index;

    index = 0;
    while((index <= length) && (data[index].key < key)){
        index++;
    }

    length++;
    for(int i = length; i > index; i--){
        data[i] = data[i-1];
    }

     data[index].key = key;
     data[index].name = name;
}

ItemType SortedList::current() const
{
    ItemType item;
    item = data[currentPos];
    return item;
}

void SortedList::ResetList()
{
    currentPos = 0;
}

void SortedList::advance()
{
    currentPos++;
}

bool SortedList::IsEmpty() const
{
    return (length == 0);
}

bool SortedList::IsFull() const
{
    return (length == MAX_LENGTH);
}

int SortedList::GetLength() const
{
    return length;
}

bool SortedList::HasNext() const
{
    return (currentPos != length);
}


main.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
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <string>
#include "sortedList.cpp"

using namespace std;

void GetList(ifstream&, SortedList&);
void PrintList(ofstream&, SortedList);

int main(){

    ifstream inputData;
    ofstream outputData;
    SortedList list;

    inputData.open("input.txt");
    if(not inputData){
        cout << "failure to open Inputbands.txt" << endl;
        system ("pause");
        return 1;
    }

    outputData.open("output.txt");
    if(not outputData){
        cout << "failure to open Outputbands.txt" << endl;
        system ("pause");
        return 1;   
    }

    GetList(inputData, list);
    PrintList(outputData, list);

    inputData.close();
    outputData.close();
    system ("pause");
    return 0;
}

void GetList(ifstream& inputData, SortedList& list){

    int key;
    string name;

    while(inputData >> key >> name){
        list.Insert(key, name);
    }

}

void PrintList(ofstream& outputData, SortedList list){

    list.ResetList();

    while(list.HasNext()){

        list.current();
        // outputData >> list.current(); >> endl; //something like this???
        list.advance();

    }
}


input.txt
1
2
3
4
5
6
7
8
9
10
11
65 Cantor
25 Pythagoras
55 Lobachevski
35 Pascal
45 Aristotle
15 Russell
38 Leibniz
27 Newton
47 Euclid
40 Whitehead
20 Reimann
To use iterators for your class you will have to define them first.

Look here http://stackoverflow.com/questions/148540/creating-my-own-iterators
and her for very detailed writeup http://accu.org/index.php/journals/1527
Topic archived. No new replies allowed.