Read file and Insert sort in one function

Hi there,

I'm supposed to a data file and Insert sort at the same time. the insertion must be in an empty array. I'm having trouble the Insert sort. It seems that the function is reading, but not sorting. can anyone tell what is going wrong?
here is my code, Thank you in advance.
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
#include <string.h>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

int const MAXSIZE = 100;	    // maximum number of records in total
int const MAXLENGTH = 31;	    // maximum string length 
int const MAXGRADE = 100;           // highest possible grade
int const LOWGRADE = 0;             // lowest possible grade

struct StudentType  {		    // information of one student
	int grade;		    // the grade of the person
	char last[MAXLENGTH];	    // last name (MAXLENGTH-1 at most)
	char first[MAXLENGTH];	    // first name (MAXLENGTH-1 at most)
};

// prototypes:
bool sortInput(istream&, StudentType [], int&);
void printList(const StudentType [], int);

//------------------------------- main ----------------------------------------
int main()  
{
   StudentType students[MAXSIZE];   // list of max of MAXSIZE number of students
   int size = 0;                    // total number of students

   // creates file object and opens the data file
   ifstream infile("data1.txt");
   if (!infile)  
   { 
      cout << "File could not be opened." << endl; 
      return 1;  
   }
 
 // read and sort input by last then first name
 
   bool successfulRead = sortInput(infile, students, size);              

  if (successfulRead)  
   { 
		printList(students, size);
   }
   return 0;
}

// ----------------------------------------------------------------------------
// functions:

/*name and grade are read into a temporary location. 
  loopes are from the end of the filled part of the array down to the beginning*/

// read and insert sort in one function.
 
bool sortInput(istream& infile, StudentType students[], int& size)
{
	int count2 = 0;
	StudentType temp;
	while (size < MAXSIZE && !infile.eof())
	{
		infile >> temp.last >> temp.first >> temp.grade;
		students[size] = temp;
		size++;
		
		for (count2 = size-1; count2 > 0 ; count2--)
		{ 
			if(strcmp(students[count2].last, temp.last) > 0)
				students[count2+1] = students[count2];
			else if(strcmp(students[count2].last, temp.last) == 0 && strcmp(students[count2].first, temp.first) > 0)
				students[count2+1] = students[count2];
			else break; 
			students[count2] = temp;
		}
	}
	return true;
}

void printList(const StudentType students[], int size) 
{
   for (int i=0; i <= size; i++)  
      cout << setw(4) << students[i].grade 
	       << " " << students[i].last 
		   << " " << students[i].first << endl;
}
Topic archived. No new replies allowed.