Trying to figure out how to write something in a function

This is a homework assignment that I finished. I am trying to find a way to improve it to become a better programmer. The point of the project is to read two files transfer it to a third and then sort it and print it on screen. I did this by loading the first two files into a third, loading the third, sort it and then loading this into a forth. When I was looking over my code I noticed in main that I am repeating code. I am trying to figure out how to put it in a function, but have not been successfull. Tips are great, examples are better. I am not supposed to use classes. Under this I just posted main, if you want to see the entire project scroll down the page.
Thanks

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
int main()
{
	ifstream inFile;
	ifstream inFile1;
	ofstream outputFile;
	outputFile.open("File4.txt");
	string *ptr =NULL;
	char *ptr1 =NULL;
	int size;
	int size1;
	int size2;

	inFile.open("File1.txt");
	inFile >> size;
	ptr = new string[size];
	ptr1 = new char[size];
	display(ptr, ptr1, inFile, size);
	cout<<endl;
	putInPointer(ptr, ptr1, inFile, outputFile, size);

	inFile1.open("File2.txt");
	inFile1 >> size1;
	ptr = new string[size1];
	ptr1 = new char[size1];
	display(ptr, ptr1, inFile1, size1);
	cout<<endl;
	putInPointer(ptr, ptr1, inFile1, outputFile, size1);

	outputFile.close();
	ifstream inFile2;
	inFile2.open("File4.txt");
	outputFile.open("File3.txt");
	size2=size1+size;
	ptr = new string[size2];
	ptr1 = new char[size2];
	beforeSort(ptr, ptr1, inFile2, size2);

	sort(ptr, ptr1, size2);
	putInPointer(ptr, ptr1, inFile2, outputFile, size2);
	display(ptr, ptr1, inFile2, size2);
	inFile.close();
	inFile1.close();
	inFile2.close();
	outputFile.close();
	delete [] ptr;
	delete [] ptr1;
	return 0;
}




Here is the entire program

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//Aaron Katz
//CSSE 152
//Program 1


# include <iostream>
# include <fstream>
# include <string>
using namespace std;

void putInPointer(string *names, char * grade, ifstream &file, ofstream &file1, int size);
// puts information from file into pointers.
// pre: infomation from file
// post: pointer with information from file

void display(string *names, char *grade, ifstream &file, int size);
// displays names and grades
// pre: students names and scores
// post: none

void sort(string *names, char *scores, int count);
// sorts names alphabetically
// pre: students names and grades
// post: ordered names and grades

void beforeSort(string *names, char *grade, ifstream &file, int size);
// puts information into one file
// pre: all students names and grades
// post: none

int main()
{
	ifstream inFile;
	ifstream inFile1;
	ofstream outputFile;
	outputFile.open("File4.txt");
	string *ptr =NULL;
	char *ptr1 =NULL;
	int size;
	int size1;
	int size2;

	inFile.open("File1.txt");
	inFile >> size;
	ptr = new string[size];
	ptr1 = new char[size];
	display(ptr, ptr1, inFile, size);
	cout<<endl;
	putInPointer(ptr, ptr1, inFile, outputFile, size);

	inFile1.open("File2.txt");
	inFile1 >> size1;
	ptr = new string[size1];
	ptr1 = new char[size1];
	display(ptr, ptr1, inFile1, size1);
	cout<<endl;
	putInPointer(ptr, ptr1, inFile1, outputFile, size1);

	outputFile.close();
	ifstream inFile2;
	inFile2.open("File4.txt");
	outputFile.open("File3.txt");
	size2=size1+size;
	ptr = new string[size2];
	ptr1 = new char[size2];
	beforeSort(ptr, ptr1, inFile2, size2);

	sort(ptr, ptr1, size2);
	putInPointer(ptr, ptr1, inFile2, outputFile, size2);
	display(ptr, ptr1, inFile2, size2);
	inFile.close();
	inFile1.close();
	inFile2.close();
	outputFile.close();
	delete [] ptr;
	delete [] ptr1;
	return 0;
}

void putInPointer(string *names, char *grade, ifstream &file, ofstream &file1, int size)
{
	for (int index =0; index<size; index++)
	{
		file>>*(names+index)>>*(grade+index);
	}
	for (int index =0; index<size; index++)
	{
		file1<<*(names+index)<<" "<<*(grade+index)<<endl;
	}
}

void display(string *names, char *grade, ifstream &file, int size)
{
	for (int index =0; index<size; index++)
	{
		file>>*(names+index)>>*(grade+index);
	}
	for (int index =0; index<size; index++)
	{
		cout<<*(names+index)<<" "<<*(grade+index)<<endl;
	}
}

void beforeSort(string *names, char *grade, ifstream &file, int size)
{
	for (int index =0; index<size; index++)
	{
		file>>*(names+index)>>*(grade+index);
	}
}

void sort(string *names, char *scores, int count)
{
	int startScan, minIndex, index;
	string minValue;
	char tempNames;
	for(startScan =0; startScan < count; startScan++)
	{
		minIndex=startScan;
		minValue = *(names + startScan);
		tempNames = *(scores + startScan);
		for(index = startScan + 1; index<count; index++)
		{
			if(*(names + index)< minValue)
			{
				minValue = *(names + index);
				tempNames = *(scores + index);
				minIndex=index;
			}
		}
		*(names + minIndex) = *(names + startScan);
		*(names + startScan) = minValue;
		*(scores + minIndex) = *(scores + startScan);
		*(scores + startScan) =tempNames;
	}
}


 



Topic archived. No new replies allowed.