C++ trouble with a template for a sorted list?

Program Requirements:
Let A be an array of n elements. Write a template function that takes an unsorted array of type <class T> as an input parameter and feeds back a sorted array via the input parameter. Assume the operators < and > are defined for the class T. In this question, you may hardcode n=5. You must write all functions you call, and also needs to write a main() to test the following data arrays stored in a data file (data.txt):

1 9 3 4 6
4 1 9 3 6
1.1 4.1 3.1 5.2 6.3
t d b e f

I have got my program to run but the output only shows me this:

The initial list is 1 9 3 4 6
The sorted list is

Can anyone help me fix this problem. I think that my SortedList function is correctly set up but I am not sure why it is not giving me the correct output. I was hoping you guys could locate any of my errors or give me any advice. This is my first time learning templates so any help would be appreciated. Thanks

My code:
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

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define MAX_ELEMENTS 5
template < class T > void SortedList(T data[MAX_ELEMENTS])
{
// Sort the list
    for (int count1 = 0; count1 < MAX_ELEMENTS; count1++) {
	for (int count2 = 0; count2 < MAX_ELEMENTS; count2++) {
	    if (data[count2] > data[count2 + 1]) {
		T temp = data[count2 + 1];
		data[count2 + 1] = data[count2];
		data[count2] = temp;
	    }
	}
    }
}

int main()
{
    string data[5];
    string temp;
    ifstream infile;
    infile.open("data.txt");
    if (!infile)
	cout << "Your file was not opened correctly";
    else
	while (!infile.eof()) {
	    for (int i = 0; i < MAX_ELEMENTS && !infile.eof(); i++) {
		infile >> data[i];
	    }
	    cout << "The initial list is ";
	    for (int i = 0; i < MAX_ELEMENTS; i++) {
		cout << data[i] << " ";
	    }
	    cout << endl;
	    cout << "Sorted list is " << endl;
	    SortedList(data);



	    infile.close();
	    system("pause");
	    return 0;
	}
}
Well, you don't really ever try to print that array the second time..
I revised my code and the output looks better but it is only showing me the sorted list of the first line in the text file. The output looks like this:

The sorted list is 1 3 4 6 9

I need help trying to get my program to read in the other lines 3 lines and print out the sorted list of each of them.

Here is my code:
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define MAX_ELEMENTS 5
template < class T > void SortedList(T data[MAX_ELEMENTS])
{
    // Sort the list
    for (int count1 = 0; count1 < MAX_ELEMENTS; count1++) {
	for (int count2 = 0; count2 < MAX_ELEMENTS; count2++) {
	    if (data[count2] > data[count2 + 1]) {
		T temp = data[count2 + 1];
		data[count2 + 1] = data[count2];
		data[count2] = temp;


	    }
	}
    }

}

int main()
{
    string data[5];
    string temp;
    ifstream infile;
    infile.open("data.txt");
    if (!infile)
	cout << "Your file was not opened correctly";
    else
	while (!infile.eof()) {
	    cout << "The sorted list is " << endl;
	    for (int i = 0; i < MAX_ELEMENTS && !infile.eof(); i++) {
		infile >> data[i];
	    }
	    SortedList(data);	// original code
	    for (int i = 0; i < MAX_ELEMENTS; i++)	// add this
		cout << data[i] << " ";	// add this
	    cout << endl;	// add this
	    infile.close();	// original code

	    system("pause");
	    return 0;
	}
}
Once again, it doesn't work, because you don't make it work. You close the flie, pause and exit the program inside the while loop that was supposed to read the other lines. Move those lines outside the loop and it might work..
Last edited on
Topic archived. No new replies allowed.